Overview
The WebSocket API broadcasts three types of real-time updates: depth updates, ticker updates, and trade updates. All messages are sent as JSON-encoded strings.Message Structure
All outgoing WebSocket messages follow this general structure:Depth Updates
Depth updates contain order book bid and ask data for an event. These updates are sent whenever the order book changes due to new orders, cancellations, or trades.Message Format
Channel identifier, typically in format
"event_orderbook_{eventId}"Depth update data
Event type, always
"depth"Array of bid levels
[price, quantity]. Optional - only present when bids change.Array of ask levels
[price, quantity]. Optional - only present when asks change.Event ID for this depth update
Example Message
Depth Update
Understanding Depth Data
Bids (b)
Bids (b)
Bids represent buy orders. Each entry is a tuple of
[price, quantity]:price: The price buyers are willing to pay (as string)quantity: Total quantity available at that price (as string)
Asks (a)
Asks (a)
Asks represent sell orders. Each entry is a tuple of
[price, quantity]:price: The price sellers are asking for (as string)quantity: Total quantity available at that price (as string)
Partial Updates
Partial Updates
Depth updates may contain only
b (bids changed), only a (asks changed), or both. If a field is absent, it means that side of the order book hasn’t changed.Handling Depth Updates
Ticker Updates
Ticker updates provide aggregated price and volume statistics for an event.Message Format
Channel identifier for the ticker stream
Ticker data
Event type, always
"ticker"Event ID for this ticker update
Current/close price
High price in the period
Low price in the period
Volume traded
Quote volume traded
Symbol/market identifier
Example Message
Ticker Update
Handling Ticker Updates
Trade Updates
Trade updates are broadcast when orders are matched and trades are executed.Message Format
Channel identifier for the trade stream
Trade data
Event type, always
"trade"Trade ID
Whether the buyer is the maker (true) or taker (false)
Trade price
Trade quantity
Symbol/market identifier
Example Message
Trade Update
Understanding Trade Data
Trade ID (t)
Trade ID (t)
Unique identifier for the trade. Format:
TRD_{uuid}Is Buyer Maker (m)
Is Buyer Maker (m)
true: The buyer placed a limit order that was matched (maker)false: The buyer placed a market order or aggressive limit order (taker)
Price (p)
Price (p)
The price at which the trade was executed (as number).
Quantity (q)
Quantity (q)
The quantity of contracts traded (as string).
Handling Trade Updates
Complete WebSocket Handler
Here’s a complete example that handles all message types:Type Definitions
Based onpackages/types/src/index.ts:170-209:
Implementation Notes
Source Code Reference:
- Message type definitions:
packages/types/src/index.ts:170-209 - WebSocket types:
services/wss/src/types/index.ts:18-28 - Engine types:
packages/types/src/index.ts:46-95
Message Broadcasting
WebSocket messages are broadcast via Redis pub/sub. When events occur in the matching engine:- Engine publishes message to Redis channel
- WebSocket server receives message from Redis
- Server broadcasts to all subscribed clients