Deep dive: Neon Realtime sync (simulated) #5
phase-3-guide fromneon-realtime-deep-dive·opened Adds the Neon Realtime deep dive, moved out of #4 so that PR can land without it. This PR's base is phase-3-guide, so the diff only shows the Neon work. It restores that work exactly as it was at 640fd00, after three rounds of review there.
The page: /deep-dives/neon-realtime#
- Describes
realtimeCollectionOptions()from@neon/realtime-tanstack@0.1.1(with@neon/realtime@0.1.1), written against the published package and its types.- The backend seals a SQL query, and the browser subscribes to it.
- The collection syncs eagerly from the stream.
- Writes go through your own API, which returns the Postgres transaction id; the handler then waits on
collection.utils.awaitTxId(txid).
- The real-API snippets are quoted from a verbatim copy of the package README (
src/guide/vendor/, excluded from formatting), pulled out by code fence. - The guide links to it from "How collections sync" and "What's next".
The simulation#
The page has a demo, labelled as a simulation of the package's flow, not the package itself. It runs against the fake API:
- Fake server: every note write gets a transaction id and reports the rows it changed.
PATCH /api/live/notes/:idsaves the change and answers{ txid }. - Simulated stream (
src/api/fake-realtime.ts): sends the notes' complete result, then one batch per committed transaction. Messages go through one first-in-first-out queue, so they arrive in commit order even if the delay changes. - Stand-in collection (
src/db/realtime-notes.ts), built on TanStack DB 1.0's own sync functions:onUpdatesends the change and waits for its txid. The stream's batch is committed and shows up together with the end of the optimistic state, so the title never flickers.- Each note's changes are saved one at a time (
inOrder), so an older rename can't land last.
awaitTxIdand cleanup:- Txid tracking belongs to one sync run and keeps only the last txid seen.
- Each write belongs to the run it started in. Cleanup cancels the run's requests and fails every write still in progress, including one still waiting for the API's answer. A late answer joins nothing, and the next write to that note goes out right away.
awaitTxIdrejects on timeout, on cleanup, and when the collection isn't syncing.
- Demo: renames the oldest note that isn't deleted, and shows each step as it happens (optimistic update, the API's txid, the wait, the stream batch, the hand-off). If no note is left, it shows an empty state. The simulation's code is quoted through the same
#regionsnippet mechanism as the rest of the guide.
Why the package isn't installed#
@neon/realtime-tanstack@0.1.1 declares the peer dependency @tanstack/db@^0.9.0, and this app runs 1.0.0-rc.1.2, which is outside that range. So the page quotes the real API from the README, and the demo is a stand-in.
The brief says "onUpdate returns a txid". In this package the handler instead waits on collection.utils.awaitTxId(txid) with a string txid from your API, and both the page and the simulation follow the package.
Tests#
- Simulated collection: an edit stays optimistic until its txid arrives; the stream arrives before the API's answer; concurrent renames; rollback; cleanup during the wait and during the PATCH; message order when the delay changes; no unhandled rejection on immediate cleanup; abort listeners are removed.
- Sync run unit tests.
- Demo note selection, including the empty case.
On this branch, vp check, vp test (23 files, 161 tests) and vp build pass.
🤖 Generated with Claude Code
- 1
fuko closed this
Sign in to join this conversation.
no time for reviewing the content now, I'll have to write the tutorial later