> ## Documentation Index
> Fetch the complete documentation index at: https://docs.0xarchive.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Troubleshoot WebSocket reconnect and replay

> Recover a dropped or lagging 0xArchive WebSocket stream: reconnect with backoff, re-subscribe, handle slow_consumer notices, and replay what you missed.

Get a dropped or lagging stream back to a state you can trust: reconnect, restore the subscriptions you track, and fill the interval you missed from replay or REST history. A new subscription streams from the moment it is accepted, so the interval before it has to come from replay or REST history.

## Symptoms

* The socket closes, or goes quiet, and your client stops receiving data.
* After a reconnect, nothing arrives because the client never re-subscribed.
* An `error` message with `error_code` `slow_consumer` arrives, and then a subscription stops sending or the socket closes.
* A replay refuses `replay.seek`, or ignores `speed`.
* A Free account receives no live data after its first 30 days.

## Match the symptom to the cause

| Symptom | Cause and fix |
| - | - |
| Socket closed | A network failure or the idle timeout. Reconnect with capped backoff and jitter, then re-subscribe from your tracked list |
| No data after reconnect | Subscriptions belong to one socket. Send `subscribe` again on the new socket and wait for `subscribed` |
| `slow_consumer` notice | The connection fell behind the stream and messages were dropped. Read faster or subscribe to less, then fill the missed interval |
| A subscription went silent after `slow_consumer` | Persistent lag on an L4, full-depth or Lighter channel stopped that subscription. Re-subscribe to resume |
| Socket closed after `slow_consumer` (`Connection closed: client too slow to keep up with data stream`) | The socket itself stayed behind across many sends, so the server closed the connection. Reconnect, subscribe to less and read each message off the socket promptly |
| `replay.seek` refused, `speed` ignored | L4 and full-depth replay is checkpoint-anchored bulk delivery. Start a new replay at the time you need |
| No live data on Free | After an account's first 30 days, live WebSocket streams need Build. Replays of past days stay on Free |

## Reconnect and restore subscriptions

The server sends a ping frame every 30 seconds. A connection with active subscriptions can be closed after 60 seconds without activity, and one with no subscriptions after 300 seconds. Keep the subscriptions your client wants in one list, and send them again on every new socket. This Node.js client tracks Hyperliquid BTC trades, reconnects with capped exponential backoff and jitter, and records the newest trade time so it knows what it missed:

```javascript theme={"theme":"github-dark"}
import WebSocket from "ws";

const apiKey = process.env.OXARCHIVE_API_KEY;
if (!apiKey) throw new Error("Set OXARCHIVE_API_KEY");

const tracked = [{ channel: "trades", symbol: "BTC" }];  // what this client wants running
let attempt = 0;
let lastTradeAt = null;                                 // newest trade time received

function connect() {
  const ws = new WebSocket("wss://api.0xarchive.io/ws?version=2026-10-01", {
    headers: { Authorization: `Bearer ${apiKey}` }
  });

  ws.on("open", () => {
    attempt = 0;
    for (const sub of tracked) ws.send(JSON.stringify({ op: "subscribe", ...sub }));
  });

  ws.on("message", (raw) => {
    const msg = JSON.parse(raw);
    if (msg.type === "subscribed") {
      console.log("subscribed", msg.channel, msg.symbol, "missed since", lastTradeAt);
    } else if (msg.type === "data" && msg.channel === "trades") {
      lastTradeAt = Math.max(lastTradeAt ?? 0, ...msg.data.map((t) => t.time));
    } else if (msg.type === "error") {
      console.warn(msg.error_code, msg.message);
    }
  });

  ws.on("close", (code) => {
    const delay = Math.min(30_000, 1_000 * 2 ** attempt++) * (0.5 + Math.random() / 2);
    console.log("closed", code, "reconnecting in", Math.round(delay), "ms");
    setTimeout(connect, delay);
  });

  ws.on("error", (err) => console.error(err.message));
}

connect();
```

After a dropped socket, the client logs the close, waits, opens a new socket, and receives `subscribed` again with the time of the last trade it saw. Stop after a retry budget in production instead of reconnecting forever, and log the close code, retry count, and restored subscriptions. [Keep-alive](https://docs.0xarchive.io/websocket/keep-alive.md) has the full sequence.

## Handle lag notices

A connection that reads too slowly receives an `error` message with `error_code` `slow_consumer`. On Lighter, a connection that falls behind `lighter_trades` or the Lighter stats channels receives a notice counting the messages it missed, for example `Dropped ~N live lighter_trades messages for BTC`. Persistent lag stops that subscription until the client re-subscribes. A socket that stays behind across many sends on any channel is closed after a final `slow_consumer` message. What to do depends on the channel:

| Channel | After a lag notice |
| - | - |
| `lighter_trades`, `rh_lighter_trades` | The dropped trades are not resent. Fill the interval from the REST trade routes |
| `lighter_open_interest`, `lighter_funding` | Each message is a full state, so the next one resyncs. No repair needed |
| `lighter_orderbook` | Every message is a full book, and a newer book is never replaced with an older one |
| L4 channels | Mark the local book unsafe and rebuild from a checkpoint. See [Troubleshoot order book sequencing](https://docs.0xarchive.io/troubleshooting/book-sequencing.md) |
| `mempool` | The connection is closed. Reconnect, subscribe to less, and read faster; missed transactions cannot be fetched later |

Reduce the subscription set or move heavy work off the message callback before you re-subscribe, or the same lag returns.

## Fill the interval you missed

Choose by what the job needs:

* **REST history** returns the rows of a bounded window, ordered after retrieval. Request the missed trades from the last time you saw to now, page with `meta.next_cursor`, and drop rows you already hold. Hyperliquid trades return one row per side of each trade, so dedupe on `trade_id` and `side`. [Troubleshoot pagination](https://docs.0xarchive.io/troubleshooting/pagination.md) has the loop.
* **Standard replay** sends stored events in order, starting at the `start` you give. `end` is optional and defaults to now; `replay_started` reports the effective window. The replay ends with `replay_completed` and does not follow new events, so keep the live subscription for anything after it. A live subscription and a replay can run on the same connection; a second replay while one is running is refused with `conflict`.

```javascript theme={"theme":"github-dark"}
// Resume from the newest trade the live stream delivered; that trade arrives again, so dedupe
ws.send(JSON.stringify({ op: "replay", channel: "trades", symbol: "BTC", start: lastTradeAt, speed: 10 }));
```

Lighter trade replay returns finalized trades only, up to `meta.finalized_through`, so the newest Lighter trades come from `/v1/lighter/trades/{symbol}/recent` instead.

## L4 and full-depth replay rules

L4 replay (`l4_diffs`, `l4_orders` and the `spot_`, `hip3_` and `hip4_` variants) and full-depth replay (`orderbook_full`, `hip3_orderbook_full`) follow different rules from standard replay:

* The server anchors the run to the latest checkpoint at or before `start` and opens with an `l4_snapshot`.
* `l4_batch` messages follow: up to 5,000 order events per batch in block order on the L4 channels, and price-level changes in 100 ms windows of event time on the full-depth channels.
* The stream can include updates before your `start`, to roll the checkpoint forward. Apply them before you score the study window.
* `speed` is ignored, and each run is single-channel.
* `replay.seek` is refused with `unsupported_for_venue`: `replay.seek is not supported for L4 channels (restart the replay at the desired start instead)`. Send a new `replay` with the new `start`.

## Verify the fix

* Every tracked subscription has a `subscribed` acknowledgement on the current socket.
* The filled interval runs from the last delivered time to the first live message, with no duplicate keys.
* A replay ended with `replay_completed`, and any `gap_detected` event is recorded with the output.

## Related

* [WebSocket connection](https://docs.0xarchive.io/websocket/connection.md): authentication and connection state
* [WebSocket replay](https://docs.0xarchive.io/websocket/replay.md): replay commands, events, and run manifests
* [Lighter channels](https://docs.0xarchive.io/websocket/lighter.md#errors-and-lag-notices): every Lighter notice and stop message
* [WebSocket limits](https://docs.0xarchive.io/websocket/limits.md): subscriptions, connections, and standard replay speed by plan

## Next step

**Bring a local book back in line**

After a reconnect or a lag notice, rebuild the order book from a checkpoint and apply diffs in block order.

[Rebuild the book](https://docs.0xarchive.io/troubleshooting/book-sequencing.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.