Connection Management
Best practices for maintaining reliable WebSocket connections.Subscription Management
Subscribe
Subscribe to one or multiple streams:Unsubscribe
Unsubscribe using the topic string:Reconnection Strategy
WebSocket connections can drop due to network issues. Always implement reconnection logic.Ping-Pong Keepalive (Required)
The server uses an explicit WebSocket ping/pong liveness check. You must respond to server pings with pongs or the connection will be closed.| Behavior | Detail |
|---|---|
| Ping interval | Server sends a WebSocket ping frame every 30 seconds |
| Pong requirement | Client must reply with a WebSocket pong frame |
| Timeout | If a pong is still missing 10 seconds after a ping, the server disconnects the connection |
| Frame type | Ping/pong are transport-level WebSocket frames, not application (JSON) messages |
- Respond immediately to ping frames with pong. Many WebSocket client libraries (e.g. Node.js
ws) do this automatically; if yours does not, handle thepingevent and send a pong. - Do not treat ping/pong as application messages—they are separate from
subscribe,post, and data messages. - If you are disconnected due to a missed pong, reconnect and resubscribe to your topics.
Rate Limits
Best Practices
Always implement reconnection logic
Always implement reconnection logic
Network issues are inevitable. Your application should automatically reconnect with exponential backoff.
Store subscriptions for resubscription
Store subscriptions for resubscription
After reconnecting, you must resubscribe to all channels. Store your subscription list.
Handle message ordering
Handle message ordering
Messages may arrive out of order during high load. Use timestamps and sequence numbers.
Respond to server ping with pong
Respond to server ping with pong
The server sends a ping every 30 seconds. If the client does not reply with a pong within 10 seconds, the server disconnects. Use a client that responds to ping (or handle it explicitly) and always implement reconnection.
Implement timeouts
Implement timeouts
If no application message is received for a long period, consider the connection stale and reconnect. Note: the server will already disconnect if pong is not sent within 10 seconds of a ping.
Use compression
Use compression
Enable per-message deflate for bandwidth savings on high-frequency streams.
Separate connections for trading
Separate connections for trading
Use one connection for market data and another for trading to avoid mixing concerns.
Connection States
Monitor connection state to handle different scenarios:| State | Description | Action |
|---|---|---|
CONNECTING | Initial connection | Wait for open event |
OPEN | Connected and ready | Can send/receive messages |
CLOSING | Connection closing | Stop sending messages |
CLOSED | Connection closed | Reconnect if needed |
Error Codes
Common WebSocket close codes:| Code | Reason | Action |
|---|---|---|
| 1000 | Normal closure | No action needed |
| 1001 | Going away | Reconnect |
| 1002 | Protocol error | Check message format |
| 1003 | Unsupported data | Check message content |
| 1006 | Abnormal closure | Reconnect |
| 1008 | Policy violation | Check rate limits |
| 1011 | Server error | Retry with backoff |