Best Practices
Connection Management
Automatic Reconnection
Implement automatic reconnection mechanism
Use exponential backoff strategy
Handle network interruptions gracefully
Heartbeat
Use heartbeat to keep connection alive
Send
"ping"messages periodically (e.g., every 30 seconds)Handle
"pong"responses to verify connection health
Connection State
Monitor connection state (
OPEN,CLOSING,CLOSED)Only send messages when connection is
OPENClean up resources when connection closes
Subscription Management
Subscribe Immediately
Subscribe to required data streams immediately after connection is established
Don't wait for connection to be fully ready before subscribing
Unsubscribe Before Disconnect
Unsubscribe from all subscriptions before disconnecting
This helps clean up server-side resources
Request ID Management
Use unique request IDs to track subscription status
Map request IDs to subscription types for easier management
Handle subscription responses to confirm successful subscriptions
Resubscribe on Reconnect
Track all active subscriptions
Resubscribe to all active subscriptions after reconnection
Maintain subscription state across reconnections
Data Processing
Data Validation
Validate received data format before processing
Check for required fields in data messages
Handle missing or malformed data gracefully
Update Frequency
Handle data update frequency to avoid excessive UI refreshes
Consider debouncing or throttling UI updates
Batch updates when possible
Portfolio Subscriptions
For
portfoliosubscriptions, actively fetch the latest data after receiving notificationsDon't rely solely on WebSocket notifications for portfolio data
Use REST API endpoints to get complete portfolio information
Performance Optimization
Selective Subscriptions
Only subscribe to needed data streams
Unsubscribe from unnecessary subscriptions promptly
Avoid subscribing to the same data multiple times
Data Caching
Use data caching appropriately
Cache instrument information and market data
Invalidate cache when receiving update notifications
Memory Management
Clean up old data to prevent memory leaks
Limit the size of data buffers
Remove unused subscriptions and listeners
Security Considerations
HTTPS/WSS
Always use
wss://(secure WebSocket) in productionNever use
ws://(unencrypted WebSocket) for sensitive data
Origin Validation
Set appropriate
OriginheaderValidate origin on client side if needed
Rate Limiting
Be aware of rate limits
Don't send excessive subscription/unsubscription requests
Implement client-side rate limiting if needed
Debugging Tips
Logging
Log all sent and received messages during development
Include timestamps and connection state in logs
Use different log levels for different types of messages
Error Tracking
Track connection errors and reconnection attempts
Monitor subscription success/failure rates
Alert on persistent connection issues
Testing
Test reconnection logic thoroughly
Test with network interruptions
Test subscription/unsubscription flows
Test with invalid messages to ensure error handling works
Last updated