Sync HTTP Fallback
How the sync client keeps reading and writing over HTTP when the WebSocket drops, and what you must poll while degraded.
How the sync client keeps working when the WebSocket cannot be held, and what a
consumer must do about it. Read sync-overview.md and sync-api-reference.md
first.
Why it exists
The normal transport is a WebSocket. Real networks break it: a VPN or captive portal that blocks the WS upgrade, a WS-hostile corporate proxy, a phone with a dozing radio or low battery, a flaky uplink. When that happens the client switches to plain HTTP sync over the same REST surface, and switches back when the socket recovers. Reads and writes keep working the whole time.
The transport state machine
Two signals, mirrored in both languages:
transportMode:"socket"or"http".isSyncLive:trueonly when the socket is up and the local cache is caught up and fresh.falsemeans degraded: the local cache still reads, but it may be stale.onTransportChange((state) => ...): fires whenmodeorisSyncLivechanges.stateis{ mode, isSyncLive }. Returns an unsubscribe function.
Transitions:
- socket → http. Any non-
connectedsocket state (including a hardauth_failed, which is treated as a network block) dropsisSyncLiveto false immediately and arms a 5 second debounce. If the socket is still down after 5s,transportModeflips to"http". The debounce is armed once, so a thrashing socket does not flip-flop. - http → socket. A
connectedsocket clears the timer and setstransportModeback to"socket".isSyncLivestays false until the next successful catch-upsyncUnified()on the live socket marks it true.
TS vs Dart: where the switch lives
- TypeScript: built into
SynclibClient(@synclib-io/client). It is transparent.client.transportMode,client.isSyncLive,client.onTransportChange(...), andclient.syncUnified()already route over HTTP when degraded. You hold one object. - Dart: the switch is not inside
SyncClient. It lives in a separateSyncManageryou wrap around the client:
Drive catch-up and on-demand fetch through the manager, not the raw client.final manager = SyncManager(client)..attach(); manager.transportMode; // TransportMode.socket | .http manager.isSyncLive; manager.transportChanges.listen(...); // stream of TransportState await manager.syncUnified(); // routes over HTTP when degraded
The REST surface the fallback uses
All under /api/sync/member, authed with the same JWT (Authorization: Bearer)
plus an x-client-id header. The client derives the HTTP base from serverUrl by
swapping the ws(s) scheme for http(s). Every request carries ?topic=<channel topic> and, when known, ?env=; scoped pulls add ?scope=&scope_id=.
| Method + path | Purpose |
|---|---|
GET /api/sync/member/:table?since=&limit= | a page of rows → {rows, next_seqnum, has_more} |
POST /api/sync/member/push | push pending changes → {acks} |
GET /api/sync/member/:table/:id | one row (404 → null) |
POST /api/sync/member/:table/by-ids | many rows by id → {rows} |
syncUnifiedOverHttp() pushes each channel's pending changes, then pulls each unit
by looping fetchPage until has_more is false. It uses the same durable,
scope-keyed cursor as the socket, so a transport flip resumes cleanly with no
duplicate work.
What is automatic vs your job
Automatic:
- The mode flip and 5s debounce.
isSyncLivetracking.syncUnified()transparently routing to HTTP when degraded.- Cursor sharing across transports.
Your job (the important part): there are no live server broadcasts over HTTP. In socket mode the server pushes changes to you; in HTTP mode it cannot. So while degraded you must pull:
- Poll
syncUnified()on a timer so the app keeps catching up. A ~20s interval works well; stop it when the socket recovers. Drive it on the flip:
(Dart: the same, viaclient.onTransportChange((s) => { if (s.mode === "http") { client.syncUnified(); /* + start 20s poll */ } else { /* stop the poll */ } });manager.transportChangesandmanager.syncUnified().) - Fetch on demand for screens that need rows not yet local:
fetchPagefor a page,ensureRow/ensureRowsfor specific ids.fetchPagealways fetches; theensure*helpers are no-ops whileisSyncLive.
This poll-while-degraded obligation is the single most important thing a consumer must implement. The mode flip is automatic; running the loop is not.
Reflecting it in the UI
Read isSyncLive / transportMode and subscribe with onTransportChange (TS) or
the SyncManager stream / a ChangeNotifier (Dart) to show an "offline / catching
up / live" banner. Do not rely on useSyncStatus in @synclib-io/react for
this: it is currently stubbed and will not re-render on change.
How apps Oalo creates use it
Apps Oalo creates already follow this pattern. The React and Expo apps wire
onTransportChange to syncUnified plus a 20s poll in their sync bootstrap. The
Flutter app's SyncClientManager adopts SyncManager and does the same.
Sign up, install the CLI, and deploy your repo in a few minutes.