Maintaining an Order Book
Use the HTTP API to load a complete order-book snapshot and the market WebSocket to apply real-time changes.
The order-book snapshot and every market update include a sequence ID.
A reliable client should use the sequence ID to:
Ignore events already included in the snapshot
Apply updates in order
Detect missing events
Determine when a complete resynchronization is required
Order-book data flow
Subscribe to market updates
↓
Buffer incoming updates
↓
Load the HTTP order-book snapshot
↓
Discard buffered updates at or below the snapshot sequence ID
↓
Apply later updates in sequence
↓
Reload the snapshot after a gap or resync notificationCreate the clients
Identify the market
Order books are identified by:
assetIdepoch
The market WebSocket subscription uses assetId.
The HTTP order-book snapshot uses both assetId and epoch.
Subscribe before loading the snapshot
Subscribe first and temporarily buffer updates.
This prevents updates that occur during the HTTP request from being missed.
MarketUpdate represents the market-update type exported by the SDK. Use the SDK’s actual exported type name in the implementation.
Load the initial snapshot
Example response:
Record the snapshot sequence ID:
Apply buffered updates
Discard updates that are already represented by the snapshot.
Apply the remaining events in sequence:
Validate sequence IDs
Before applying an update, confirm that it follows the last applied update.
This example assumes sequence IDs increase by one for each applicable market event. Confirm that behavior against the SDK’s exported market-update contract before enforcing it in production.
At minimum, clients should reject updates that move backward and resynchronize whenever continuity cannot be established.
Market update types
The market WebSocket publishes four update types.
Order
Trade
Cancel
Resolution
The development team’s WebSocket document does not define the fields inside data.
The exact order-book mutation logic should be implemented from the SDK’s exported event types rather than inferred from the empty examples.
Use specific handlers
Applications can use one general handler:
They can also use event-specific handlers:
Do not apply the same update through both onUpdate and an event-specific handler.
Use onUpdate for one centralized order-book reducer, or use the specific handlers without also processing the general callback.
Resynchronize the order book
Reload the entire snapshot when:
onResyncRequiredis calledA sequence gap is detected
The connection reconnects
An update cannot be applied
The local book fails an integrity check
The market epoch changes
In production, retain updates received while the snapshot request is in progress. Do not clear the same buffer after new events have been added to it.
A safer implementation swaps active buffers:
Top-of-book applications
Applications that only need the best bid and ask can use:
Response:
The same resynchronization principles apply: reload the HTTP value after a disconnect or sequence failure.
Account-owned orders
Use getBookOrders to load resting orders for one account.
Response:
Reload this snapshot after a resynchronization event if the application maintains a separate account-order view.
Unsubscribe
The function returned by subscribeOrderBook removes only the handler created by that subscription call.
To remove every local handler for an asset:
Close the socket when the application no longer needs market updates:
Recommended safeguards
A production order-book consumer should:
Buffer events during snapshot loading
Track the last applied sequence ID
Ignore duplicate or stale events
Detect sequence discontinuity
Allow only one resynchronization at a time
Reload after
onResyncRequiredValidate that bid and ask levels remain sorted
Validate that sizes do not become negative
Replace the local state atomically after a new snapshot
Record resynchronization failures and retry with backoff
Last updated