# How to replay missed WebSocket messages after reconnecting

URL: https://docs.folkways.io/docs/guides/replay-missed-websocket-messages

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 [#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 [#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**

```ts
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 [#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 [#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 [#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 [#related-documentation]

- [Quick start](/docs/js-sdk/quick-start)
- [Subscribing and gaps](/docs/js-sdk/subscribing)
- [Connection and reconnects](/docs/js-sdk/connection)
- [Offline and local storage](/docs/js-sdk/local-storage)

[Create a free workspace](https://app.folkways.io/signup) and follow the [quick start](/docs/js-sdk/quick-start) to try this pattern.

- [stream](https://folkways.io/products/stream)
- [notifications](https://folkways.io/products/notifications)
- [AI streaming](https://folkways.io/use-cases/ai-streaming)
