Skip to content

Build

How Sync works

A source is a piece of shared state, such as a board or counter. Source.make defines the kind of data and the actions clients can request. An ID selects one instance: authorized clients connected to the same board ID see the same board, while another ID identifies a different board.

The server holds the authoritative state for each source. A client keeps a local copy, or replica. A client starts from a snapshot of the shared view, then applies events from the server to keep the view current.

One source, one server authority. The client sends typed actions to the Sync server and receives snapshots and events. The server reads and commits authoritative storage. An optional local journal retains pending client edits across reloads.
  • Yielded Sync: Sync clientSend actions, receive updates
  • External: Local journalOptional: retain pending edits
  • Yielded Sync: Sync serverAuthorize, commit, publish
  • External: Server storageState, events, receipts
  • Sync client to Sync server
  • Sync server to Sync client
  • Sync client to Local journal
  • Local journal to Sync client
  • Sync server to Server storage
  • Server storage to Sync server
  • Browser: Sync client, Local journal
  • Server: Sync server, Server storage
Piece Your code Sync handles
Shared contract Source.make defines snapshots, events, and actions Encoding, validation, and typed calls
Server Server.make defines state, authorization, and action handlers Atomic commits, receipts, and ordered replay
Client Client.definition applies events and optionally predicts actions Connections, replicas, and recovery

The getting started guide connects these pieces for a counter. Your application also supplies authentication, the UI, and any external side effects. All authorized viewers of one source receive the same public snapshot; keep viewer-private data behind a separate source boundary.

When a client sends an action, the server authorizes it and runs your handler. The handler returns the new state, events for clients, and a result for the caller. Sync commits the state, events, and a receipt together before publishing. The result and the event can arrive at the calling client independently.

An optimistic reducer can show a prediction before the server replies. Handlers run one at a time per source, and your app decides how to handle conflicting edits. To share a change from elsewhere in your app, submit an action to the source.

The scenarios below follow three clients reserving from a source with three seats. reserve takes the next free seat or fails with SoldOut.

Three clients share one source with three seats. Nobody is connected yet.

A client that falls behind retained history loads a fresh snapshot instead of replaying events. Presence and other ephemeral messages can be missed and never enter the durable history. See retry and recovery for the client operations.

Storage What it keeps
Server storage Authoritative state, retained events, and receipts
Client journal Pending commands and their identities across reloads
Client snapshot cache A starting view while the client reconnects

Client persistence is optional. The cache can be discarded, but keep the journal when repairing a cache or changing storage so unresolved commands can be reconciled. See persistence for browser setup.