Live feeds
A chart that follows the market needs the newest bars as they form. New data reaches the cache in one of two ways, the watermark keeps the forming bar honest, and cacheChanged tells every listening tab where the cache changed (Change events).
The forming bar is provisional
Report finalizedUntil (the first timestamp that is not final) with every batch and response. The forming bar is then stored and shown, but never covered, so the next fetching read over it asks the backend again, and a bar that closed while the page was away is mended (Versions and watermarks).
Pushing: Server-Sent Events or a WebSocket
A push feed brings bars; put each as a batch without a range. Such a put adds or overwrites the bars it carries and removes nothing, and claims coverage only for its own span below the watermark.
/** Puts every bar the backend streams; returns the stop. */
export function followStream(cache: CacheHandle, redraw: () => void) {
// Server-Sent Events: the backend names each message `bar` (onmessage
// hears only unnamed ones). Its data is a put batch, the bar forming now
// (and the bar that just closed, when one did); its meta.finalizedUntil
// keeps the forming bar provisional, so a fetching read asks for it again.
const events = new EventSource(`/api/stream/crypto/BTC?interval=${MINUTE}`);
events.addEventListener("bar", (event) => {
const batch = JSON.parse((event as MessageEvent<string>).data) as PutBatch;
// No range: adds or overwrites these bars and removes nothing.
void cache.put(batch).then(redraw);
});
return () => events.close();
}A WebSocket works the same way. Unlike EventSource, it does not reconnect by itself, and it can present a token in its first message, which an EventSource cannot send as a header.
Polling
No push endpoint at all: read the newest stretch with a normal, fetching get on a timer. The cache fetches only what it lacks there, which is the provisional end and any bar closed since.
/** Reads the newest bars with a fetching get, every two seconds. */
export function startPolling(
cache: CacheHandle,
redraw: () => void,
everyMs = 2000,
) {
// Only what the cache lacks is fetched: the provisional end and any bar
// closed since. End on the bar forming now: an end of Date.now() snaps
// outward to the next bar, which does not exist yet.
const timer = setInterval(() => {
const forming = Math.floor(Date.now() / MINUTE) * MINUTE;
void cache.get({ start: forming - 5 * MINUTE, end: forming }).then(redraw);
}, everyMs);
return () => clearInterval(timer);
}Many tabs, one feed
Every tab with its own feed multiplies the backend's connections. The reference clients elect one tab per browser with a Web Lock to hold the feed: it puts each bar into the cache. With a SharedWorker every tab, the holder included, hears the put as cacheChanged and reads the bar back from the same cache with cacheOnly, without a fetch. With a dedicated worker or in-process, each tab has a cache of its own and hears only its own changes, so the holder passes each bar on a BroadcastChannel and every tab puts it into its own cache. When the holder closes, the lock passes to another tab. Each time a feed opens again (the channel tells every tab), a tab that follows the market reads its newest bars with a fetch, which mends what the feed missed while it was down.
reference/shared/src/live.ts implements the election and the three sources (sse, ws, poll); reference/README.md describes the behaviour and its limits.
Catching up after a gap
A pushed batch covers only its own span. If a feed drops for a while, the bars it missed stay uncovered, and a fetching get over that stretch fills them. Note that a put without a range claims its own span, from its first to its last timestamp: a batch that skips bars between those two confirms the skipped slots as gaps. Push one bar, or one unbroken run of bars, per batch.
Redrawing when the cache changes
A view redraws on cacheChanged: a put from any tab, a fetch any tab's get started, a hydrate, an invalidate, a watermark moved back and a clear each send one, to every tab that listens, the writer's own included. Read the range on screen again by the read rule: with cacheOnly after an event that wrote something, with a fetch after one that only withdrew coverage or cleared. The forming bar is written on every feed tick, so a fetch on every event would loop; the rule never fetches because of a fetch. Coalesce a burst of ticks into one read per animation frame, as the integrations do.
Known limit: one cache per tab outside a SharedWorker
With a dedicated worker or in-process, each tab has a cache of its own, and cacheChanged reports only that tab's changes. Another tab's put never reaches it: the application has to send the data, as the reference clients do with a BroadcastChannel, or invalidate the range and fetch it again from an updated backend. A fetching get on a timer (Polling) still brings each tab new and provisional bars from the backend. A worker script from before change events sends none at all; check client.features.has('cacheChanged') and poll where it is missing.