Skip to content

Lesson 32 — Realtime sync, coalescing, and bounded replay

Outcome

Make live activity responsive and reconnect safely without replaying an unbounded provider firehose.

Why this comes now

Comparable tools have frozen clients and displayed stale state even while transport remained connected. We now have enough real activity volume to build this correctly.

Understand

Separate raw storage, live stream, and historical read model. Initial state arrives as a snapshot/query; live events advance it. Small reconnect gaps can replay in batches; large gaps use a fresh snapshot. Track Connection Health separately from Sync Health because a subscription can die while the socket stays alive.

Coalesce updates before publishing to React. Replaying 2,000 token deltas as 2,000 full state publications is avoidable work.

Build the real project

  1. Add sequence/cursor semantics for relevant UI update stream.
  2. Implement bounded resume gap and snapshot fallback.
  3. Handle cursor-ahead invalidation.
  4. Subscribe/buffer around snapshot so no race loses events.
  5. Batch/coalesce replay and high-frequency live deltas.
  6. Add ConnectionStatus + SyncStatus.
  7. Create deterministic large-history perf test/playground.
  8. Simulate killed subscription while connection remains and prove client detects/re-hydrates.

Completion gate

Large-gap reopen does not replay every raw event. UI publication count is bounded/batched. CONNECTED + STALE can be represented and recovered. Performance test prevents obvious quadratic regression.

Pitfalls to avoid

Do not equate WebSocket/HTTP connection alive with data current. Do not use subscriptions as historical pagination API. Do not derive entire timeline from scratch on every token delta.

References

T3 replay bug: https://github.com/pingdotgg/t3code/issues/4596 ; stale subscription: https://github.com/pingdotgg/t3code/issues/4589 ; stale remote projection: https://github.com/pingdotgg/t3code/issues/5742 ; REALTIME_SYNC.md.

Checkpoint

M12 is complete after a long fake history and a real provider history both reopen smoothly.

Forge is local-first. The docs are part of the product engineering system.