Restore WebSocket subscriptions after a disconnect

A reconnect restores transport first. Before consuming events again, restore subscriptions and rebuild usable state. Keep distinct states for connecting, subscribed, synchronizing and ready.

Replay a disconnect in the middle of an update

Imagine the last verified book update was 105, then the connection drops. If the next session begins with a fresh snapshot at 110, initialize from that snapshot and apply compatible later updates in the prescribed order. Discard saved entries after integrity becomes uncertain. A live private order channel can also miss fills, so reconcile open orders and recent executions before marking the private state ready.

  1. Detect disconnect and silence using heartbeat and timeout rules for the venue.
  2. Retry with capped backoff and jitter. Keep at most one active reconnect loop and respect the venue’s connection policy.
  3. Reissue desired subscriptions and wait for acknowledgements. Reject callbacks from the superseded connection.
  4. Follow the channel’s snapshot or resume procedure and only then release updated state.

Test silence, socket close and a stalled acknowledgement separately. An open socket can still lack data. Record the time from losing valid state to recovering it, and keep the strategy informed during the gap. Kraken reconnection guide · Rebuild an order book.