Error Handling
Connection Errors
If the connection fails, the WebSocket will trigger an error event. Clients should implement a reconnection mechanism.
Common Connection Issues
403 Forbidden Error:
This usually indicates that the server rejected the connection due to missing or invalid headers
Solution: Ensure you include a
User-Agentheader and optionally anOriginheader when connectingNode.js clients using the
wslibrary must explicitly set these headers
Connection Timeout:
Check your network connection and firewall settings
Verify the WebSocket URL is correct
Unexpected Server Response:
Ensure you're using the correct protocol (
wss://for secure connections)Verify the server endpoint is accessible
Message Format Errors
If the message format is incorrect, the server may not respond or return an error. Please ensure:
JSON format is correct
All required parameters are provided
Parameter types are correct (e.g.,
chainIdis a number,instrumentis a string)methodis either"SUBSCRIBE"or"UNSUBSCRIBE"typematches one of the supported subscription types
Reconnection Strategy
When implementing reconnection logic, consider:
Exponential Backoff: Start with a short delay (e.g., 1 second) and increase it exponentially (2s, 4s, 8s, etc.) up to a maximum delay
Max Retries: Set a maximum number of reconnection attempts before giving up
Resubscribe: After reconnecting, resubscribe to all previously active subscriptions
State Management: Track subscription state to know what to resubscribe to
Error Response Format
If the server returns an error, it may follow this format:
Best Practices
Always validate incoming messages before processing
Implement proper error logging
Handle network interruptions gracefully
Test reconnection logic thoroughly
Monitor connection health using heartbeat mechanism
Last updated