Skip to Content
LearnArchitectureSync Protocol

Sync Protocol

The wire protocol that keeps client and server synchronized.

Wire Format

WireRow (Bootstrap)

Data format for bootstrap:

interface WireRow { t: string // Type name ('user', 'post') id: string // Record ID v: number // Version number p: Record<string, any> // Payload (field values) }

Example:

{ "t": "post", "id": "post-123", "v": 5, "p": { "title": "My Post", "content": "Hello world", "authorId": "user-1", "createdAt": 1698765432000 } }

SyncFrame (Delta/Real-Time)

Change format for incremental sync:

interface SyncFrame { sid: number // Sync ID (monotonic) t: string // Type name id: string // Record ID op: 'i'|'u'|'d' // Operation (insert, update, delete) p?: any // Patch (for i/u) who?: string // Actor ID at: number // Timestamp ctx?: { // Context client_tx_id?: string } }

Example:

{ "sid": 1001, "t": "post", "id": "post-123", "op": "u", "p": { "title": "Updated Title" }, "who": "user-1", "at": 1698765432000, "ctx": { "client_tx_id": "tx-789" } }

Endpoints

GET /sync/v1/bootstrap

Initial full data load:

Request:

GET /sync/v1/bootstrap Authorization: Bearer JWT_TOKEN

Response:

Content-Type: application/x-ndjson {"t":"user","id":"u1","v":1,"p":{...}} {"t":"post","id":"p1","v":3,"p":{...}} {"t":"post","id":"p2","v":1,"p":{...}}

Format: Newline-delimited JSON (NDJSON) for streaming.

GET /sync/v1/delta

Incremental updates:

Request:

GET /sync/v1/delta?since=1000 Authorization: Bearer JWT_TOKEN

Response:

Content-Type: application/x-ndjson {"sid":1001,"t":"post","id":"p1","op":"u","p":{"title":"Updated"},"who":"u1","at":1698765432000} {"sid":1002,"t":"user","id":"u2","op":"i","p":{...},"who":"u1","at":1698765433000}

GET /sync/v1/ws

WebSocket connection:

Request:

GET /sync/v1/ws?token=JWT_TOKEN Upgrade: websocket

Server Pushes:

[{"sid":1003,"t":"post","id":"p1","op":"u","p":{"title":"Real-time Update"},"at":1698765434000}]

Client Acknowledges:

(No explicit ack - client applies and continues)

POST /sync/v1/tx

Mutations:

Request:

POST /sync/v1/tx Authorization: Bearer JWT_TOKEN Content-Type: application/json { "ops": [ { "t": "post", "id": "p1", "op": "u", "patch": { "title": "New Title" }, "baseVersion": 3, "clientTxId": "tx-456" } ] }

Response:

{ "ok": true, "results": [ { "ok": true, "id": "p1", "version": 4, "clientTxId": "tx-456" } ] }

Next Steps

Last updated on