Introduction
The Opinix Trade WebSocket API provides real-time market data updates including order book depth, trades, and ticker information. WebSocket connections enable low-latency, bi-directional communication for live trading data.Connection
WebSocket URL
Connect to the WebSocket server at:Establishing Connection
Subscription Model
The WebSocket API uses a subscription-based model where clients subscribe to specific channels to receive updates.User Management
When a client connects:- A unique user ID is automatically generated (source:
services/wss/src/classes/UserManager.ts:38-40) - The client is registered in the UserManager singleton
- Upon disconnect, all subscriptions are automatically cleaned up
Subscription Manager
The WebSocket server uses Redis pub/sub to efficiently manage subscriptions:- Each subscription channel corresponds to an event ID
- Multiple users can subscribe to the same channel
- When the first user subscribes to a channel, the server subscribes to the Redis channel
- When the last user unsubscribes, the server unsubscribes from Redis
Message Types
The WebSocket API supports two types of messages:Incoming Messages (Client → Server)
Subscribe
Subscribe to orderbook updates for specific events
Unsubscribe
Unsubscribe from orderbook updates
Outgoing Messages (Server → Client)
Subscribe to Orderbook
To receive orderbook updates for specific events, send a subscribe message:Message Format
Must be
"subscribe_orderbook"Array of event IDs to subscribe to
Example
Unsubscribe from Orderbook
To stop receiving updates for specific events:Message Format
Must be
"unsubscribe_orderbook"Array of event IDs to unsubscribe from
Example
Connection Lifecycle
Best Practices
Implement Reconnection Logic
Implement Reconnection Logic
WebSocket connections can drop due to network issues. Always implement automatic reconnection with exponential backoff:
Handle Message Parsing Errors
Handle Message Parsing Errors
Always wrap JSON parsing in try-catch to handle malformed messages:
Subscribe Only to Needed Events
Subscribe Only to Needed Events
Don’t subscribe to all events. Only subscribe to the events you’re actively displaying to reduce bandwidth and processing overhead.
Unsubscribe When Done
Unsubscribe When Done
When navigating away from an event page or closing a view, unsubscribe from events you no longer need.
Monitor Connection Health
Monitor Connection Health
Implement ping/pong or heartbeat mechanism to detect stale connections:
Type Definitions
Based onservices/wss/src/types/index.ts, here are the TypeScript type definitions:
Implementation Notes
Source Code Reference:
- WebSocket server:
services/wss/src/index.ts - User management:
services/wss/src/classes/UserManager.ts - Subscription management:
services/wss/src/classes/SubscriptionManager.ts - Type definitions:
services/wss/src/types/index.ts - Message types:
packages/types/src/index.ts:170-209