Best Practices

Connection Management

  1. Automatic Reconnection

    • Implement automatic reconnection mechanism

    • Use exponential backoff strategy

    • Handle network interruptions gracefully

  2. Heartbeat

    • Use heartbeat to keep connection alive

    • Send "ping" messages periodically (e.g., every 30 seconds)

    • Handle "pong" responses to verify connection health

  3. Connection State

    • Monitor connection state (OPEN, CLOSING, CLOSED)

    • Only send messages when connection is OPEN

    • Clean up resources when connection closes

Subscription Management

  1. Subscribe Immediately

    • Subscribe to required data streams immediately after connection is established

    • Don't wait for connection to be fully ready before subscribing

  2. Unsubscribe Before Disconnect

    • Unsubscribe from all subscriptions before disconnecting

    • This helps clean up server-side resources

  3. 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

  4. Resubscribe on Reconnect

    • Track all active subscriptions

    • Resubscribe to all active subscriptions after reconnection

    • Maintain subscription state across reconnections

Data Processing

  1. Data Validation

    • Validate received data format before processing

    • Check for required fields in data messages

    • Handle missing or malformed data gracefully

  2. Update Frequency

    • Handle data update frequency to avoid excessive UI refreshes

    • Consider debouncing or throttling UI updates

    • Batch updates when possible

  3. Portfolio Subscriptions

    • For portfolio subscriptions, actively fetch the latest data after receiving notifications

    • Don't rely solely on WebSocket notifications for portfolio data

    • Use REST API endpoints to get complete portfolio information

Performance Optimization

  1. Selective Subscriptions

    • Only subscribe to needed data streams

    • Unsubscribe from unnecessary subscriptions promptly

    • Avoid subscribing to the same data multiple times

  2. Data Caching

    • Use data caching appropriately

    • Cache instrument information and market data

    • Invalidate cache when receiving update notifications

  3. Memory Management

    • Clean up old data to prevent memory leaks

    • Limit the size of data buffers

    • Remove unused subscriptions and listeners

Security Considerations

  1. HTTPS/WSS

    • Always use wss:// (secure WebSocket) in production

    • Never use ws:// (unencrypted WebSocket) for sensitive data

  2. Origin Validation

    • Set appropriate Origin header

    • Validate origin on client side if needed

  3. Rate Limiting

    • Be aware of rate limits

    • Don't send excessive subscription/unsubscription requests

    • Implement client-side rate limiting if needed

Debugging Tips

  1. 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

  2. Error Tracking

    • Track connection errors and reconnection attempts

    • Monitor subscription success/failure rates

    • Alert on persistent connection issues

  3. Testing

    • Test reconnection logic thoroughly

    • Test with network interruptions

    • Test subscription/unsubscription flows

    • Test with invalid messages to ensure error handling works

Last updated