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: true only when the socket is up and the local cache is caught up and fresh. false means degraded: the local cache still reads, but it may be stale.
  • onTransportChange((state) => ...): fires when mode or isSyncLive changes. state is { mode, isSyncLive }. Returns an unsubscribe function.

Transitions:

  • socket → http. Any non-connected socket state (including a hard auth_failed, which is treated as a network block) drops isSyncLive to false immediately and arms a 5 second debounce. If the socket is still down after 5s, transportMode flips to "http". The debounce is armed once, so a thrashing socket does not flip-flop.
  • http → socket. A connected socket clears the timer and sets transportMode back to "socket". isSyncLive stays false until the next successful catch-up syncUnified() 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(...), and client.syncUnified() already route over HTTP when degraded. You hold one object.
  • Dart: the switch is not inside SyncClient. It lives in a separate SyncManager you wrap around the 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
    
    Drive catch-up and on-demand fetch through the manager, not the raw client.

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 + pathPurpose
GET /api/sync/member/:table?since=&limit=a page of rows → {rows, next_seqnum, has_more}
POST /api/sync/member/pushpush pending changes → {acks}
GET /api/sync/member/:table/:idone row (404 → null)
POST /api/sync/member/:table/by-idsmany 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.
  • isSyncLive tracking.
  • 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:

  1. 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:
    client.onTransportChange((s) => {
      if (s.mode === "http") { client.syncUnified(); /* + start 20s poll */ }
      else { /* stop the poll */ }
    });
    
    (Dart: the same, via manager.transportChanges and manager.syncUnified().)
  2. Fetch on demand for screens that need rows not yet local: fetchPage for a page, ensureRow/ensureRows for specific ids. fetchPage always fetches; the ensure* helpers are no-ops while isSyncLive.

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.

Try it on your own Base

Sign up, install the CLI, and deploy your repo in a few minutes.

bring your repo