How to replay missed WebSocket messages after reconnecting
Recover missed WebSocket events with ordered history, cursors, automatic resubscription and explicit handling of retention gaps.
WebSocket reconnects restore a connection, not the messages sent while it was closed. To recover those messages, store ordered events on the server, track the last processed cursor and resume from that cursor. Folkways Stream implements this pattern within its retained history.
Why reconnect alone is not enough
Imagine a client receives events 40 and 41, loses its network, and reconnects after events 42 to 44 were published. A new socket cannot reconstruct those events unless the server kept them. A cursor identifies a position in an ordered stream; it is not the same as a connection ID.
Keep the stream order per topic. Do not assume a single global ordering across topics.
Subscribe and resume with Folkways
Install the SDK and configure a Stream topic rule for orders/42, with subscribe and history permissions for the authenticated reader. Follow the quick start for the token endpoint. Replace the example node URL with the URL from your segment. Run the following in your authenticated app.
Browser example · log the latest retained events and subsequent updates
import { Folkways } from '@folkwaysio/realtime';
const client = await Folkways.connect({
url: 'https://rte.example.com',
authUrl: '/api/realtime-token',
});
const sub = client.topic('orders/42').subscribe((msg) => {
console.log(msg.id, msg.name, msg.data);
}, { from: { last: 20 }, onGap: 'fail' });
sub.on('gap', (gap) => console.warn('Reload from source of truth', gap));
sub.on('error', (error) => console.error(error));
await sub.ready;Choose the initial replay position
The first subscription can start at the newest event, the oldest retained event, the last N events, or after a saved cursor. On an automatic reconnect, the SDK resumes after the last completed handler rather than repeating the initial subscription options.
- Use latest for updates where prior history is irrelevant.
- Use oldest or last N for recent event history.
- Use after: cursor when restoring a known application checkpoint.
Handle gaps and durable side effects
Retention is bounded by time and topic size. A gap means the requested history is no longer available. For critical workflows, stop applying incremental updates, fetch a fresh snapshot or backfill from your database, and resume from a matching checkpoint. Make processing idempotent using the event ID together with its topic; a reconnect is not a guarantee of exactly-once application side effects.
A persistent client store can preserve cursors across reloads. If the UI is rebuilt from scratch, restore matching application state too, or deliberately replay retained history instead.
Test before shipping
Open a subscriber, disconnect its network, publish several events from another authorized client and restore connectivity. Verify order and final state. Repeat with a page reload, a slow asynchronous handler and an expired cursor. Treat a retention gap as a normal recovery case rather than a silent success.
Related documentation
Create a free workspace and follow the quick start to try this pattern.
Developer guides
Practical patterns for building reliable real-time applications, replaying WebSocket events and streaming AI responses with Folkways.
How to resume an AI response stream after a page reload
Decouple AI generation from the browser connection and rebuild a response from ordered chunks after a reload or network interruption.