Disconnects and reconnecting
Why the server closes a connection
The server closes a connection itself only for the reasons below, and always sends a close code and reason so you can tell what happened.
| Close code | Reason | What to do |
|---|---|---|
| 1013 | slow consumer: reply buffer full | You did not read replies to order commands for about two seconds while they queued up. Read continuously, then reconnect and recover state. |
| 1013 | slow consumer: send buffer full | You were not reading fast enough and 256 messages queued up. The queued messages are still sent before the close; the message that did not fit, and anything after it, are not. Reconnect and recover state (see below). |
| 1013 | server at capacity | Reconnect with backoff. |
| 1008 | too many connections for this user | Your account has too many connections open (keep to 25 across all its API keys); this was the oldest. Close connections you do not need. |
| 1008 | an error code, for example ACCOUNT_LOCKED | An auth message carried a valid API key that is not allowed (see Connecting and authentication). An error with the same code is sent first. |
| 4008 | the auth_expired reason | Your credential expired, was revoked, or is no longer allowed. An auth_expired message is sent first. Reconnect with a valid credential. |
| 1006 (no close frame) | — | The network dropped, the server restarted (for example during a deploy), or nothing, not even a pong, reached the server for 60 seconds (see Keepalive below). Reconnect and recover state. |
A connection refused before it opens gets an HTTP status instead. See Connecting and authentication.
Reconnecting and recovering state
Treat any disconnect the same way:
- Reconnect with exponential backoff and jitter (for example 1s, 2s, 4s… up to 30s). Honour
Retry-Afteron a 429. - Subscribe again. Subscriptions are not restored after a reconnect.
- For orderbooks, replace your local book with the snapshot sent on subscribe. Do not merge it with the book you had before.
- User events are not replayed. Fetch what you may have missed over REST: open orders (
GET /api/trading/orders/active) and recent trades (GET /api/trading/trades). - Order commands that had no reply may or may not have been applied, and commands the server had already received still run after the connection drops. See "Commands with no reply" on Placing and cancelling orders before sending them again.
Keepalive
The server sends a WebSocket ping every 54 seconds and closes the connection, without a close frame, when nothing (not even a pong) has arrived from you for 60 seconds. On an otherwise quiet connection that means answering a ping within 6 seconds. Most WebSocket libraries answer pings automatically.
You can also check the connection yourself:
{"type":"ping"}The server answers:
{"type":"pong"}Best practices
- Read from the socket continuously and do your processing on another thread or queue. A slow message handler is the usual cause of a 1013 disconnect.
- Use one long-lived connection per purpose (for example market data and user events) instead of opening many short-lived ones.
- Subscribe only to the channels you use.
- Make your handling of order and trade events idempotent: during recovery you may see the same order or trade both over REST and on the socket.
Updated 3 days ago
Did this page help you?

