OKX SBE Market Data Integration: Decoding, Order Book Building and Disconnection Recovery

 / 
OKX
 / 
1

OKX's official public documentation does not have a dedicated standalone chapter for "SBE market data integration". All publicly available SBE-related information comes from third-party descriptions of OKX data streams: OKX provides data in SBE encoding for core spot and perpetual contract channels (msg_type 101/102/103), which shares the same sequence ID space as JSON encoding. This means you most likely need to complete SBE frame decoding adaptation on your own based on existing JSON documentation, instead of waiting for a full official SBE integration guide. Below we separate confirmed working mechanisms and parts requiring your own judgment across three stages: decoding, order book building, and disconnection recovery.

OKX Exchange
A leading global cryptocurrency platform,suitable for both beginners and experienced traders.
New user benefit: 20% off trading fees upon registration!!

First confirm which type of "SBE" you are working with

If you search for SBE in OKX's official documentation, you will find very few direct resources. A description from a third-party data integration tool on GitLab notes that OKX provides three core data types (ticker, depth, trades) in SBE encoding for spot and perpetual contracts, with msg_type values 101, 102, 103 respectively. The same source states that SBE and JSON encodings share the exact same sequence ID space, and you should consume both encodings in sync, not treat them as redundant backup data sources.

The core implication of this description is: SBE does not change the sequence logic of OKX market data, it only modifies the binary layout of data frames. The seqId field (used for ticker and depth data) and tradeId field (used for trades data) that you already use for deduplication and sorting in the JSON channel are the exact same fields present in SBE frames. If you already have a working JSON-based order book building logic, your SBE integration workload will be concentrated on the decoding layer, instead of rebuilding the full sequence processing pipeline from scratch.

One important gap to note: Details of the SBE frame layout mentioned above (field offsets, byte order, message header structure) are not documented on any publicly accessible OKX official page. To implement actual decoding, you need to apply to OKX for the SBE schema file, or get the frame definition from the SDK or data service provider you use. Never guess the binary layout based only on third-party descriptions.

Decoding: Verify logic first before handling SBE

Before your SBE decoder is ready, get the full sequence processing logic working first using the JSON channel. After subscription, OKX's books channel first pushes a full snapshot, then pushes incremental updates, with each update carrying a seqId and a prevSeqId. The prevSeqId should match the last seqId recorded locally; if they do not match, it means messages are lost or out of order in between, and your local order book is no longer trustworthy.

This validation rule applies to SBE as well, because the sequence fields are shared. Typical signs of decoding layer errors are frame parsing failures or obviously abnormal field values (for example, negative or extremely large price values), while typical signs of sequence layer errors are mismatched prevSeqId values. You need to troubleshoot these two types of issues separately: the first checks your frame layout and byte order, the second checks your data consumption speed and connection status.

One actionable judgment rule: If you receive data in SBE mode but prevSeqId values are frequently discontinuous, first check if you are mixing messages from both encodings into the same sequence space without proper sorting. The shared sequence space means SBE and JSON seqId values increment interleaved, you cannot only consume SBE and ignore messages in the same sequence range from the JSON channel, otherwise you will create artificial gaps.

Order Book Building: Only apply increments after snapshot, do not attempt to repair gaps

The correct order book building workflow for the books channel is fixed: subscribe → receive full snapshot → initialize local order book with snapshot → update each subsequent increment by price level (a quantity value of 0 means delete that price level). The books5 channel works differently: every push is a full snapshot of the top 5 order levels, no incremental state maintenance is needed, which is suitable for scenarios that only care about the best bid and ask prices. Use the books channel if your strategy needs full depth data or order imbalance calculation; books5 is simpler if you only need top-of-book prices.

When a sequence gap appears, do not try to "fill" missing levels with subsequent increments. OKX's seqId/prevSeqId mechanism does not support message retransmission, missing messages will never be resent. The correct workflow is: detect prevSeqId mismatch → discard local order book → re-subscribe to the same channel → wait for the new full snapshot → rebuild the order book from the snapshot. Third-party guides and SDK implementations all point to the same conclusion: gaps cannot be repaired, only full resync works.

One detail to note: OKX previously used checksum validation, and the 2026 migration direction is the seqId/prevSeqId mechanism. If old code or old references you use still include checksum logic, it may no longer work. Judge based on whether the messages you currently receive include the seqId field.

Disconnection Recovery: Successful reconnection does not equal you can resume trading

After a WebSocket connection drops, auto-reconnection libraries usually help you complete reconnection, re-authentication (for private channels) and re-subscription. But before the new snapshot triggered by re-subscription arrives, any order book state you hold locally is outdated. If you let your strategy make decisions based on the old book immediately in the reconnection success callback, you are trading with market state from before the disconnection for market data after the disconnection.

The condition to confirm full recovery is not "connection is open", but: new snapshot is received and applied, and the prevSeqId of the subsequent first increment is continuous with the snapshot's seqId. Before that point, your strategy should stay paused. Many SDK designs have explicit prompts for this: reset the local order book on the reconnected event, and wait for the channel to send a new snapshot.

OKX may send a notice event with code 64008 before server-side upgrades, indicating an upcoming restart. If your client handles this notification, you can start preparing for reconnection and resync before the server actively disconnects, instead of passively responding after the connection is fully dropped. This is not mandatory, but it can shorten the window where no valid data is available.

For heartbeat, OKX uses the standard ping/pong mechanism. If you do not receive any message for around 20 seconds, send a ping; if you miss two consecutive pong responses, mark the connection as dead. Most WebSocket libraries handle ping/pong at the protocol layer, but OKX also accepts the application-level string "ping". For production systems, it is recommended to implement both. Disabling heartbeat is not recommended, because silent disconnections (TCP connection appears alive but no data flows) cannot be detected without a heartbeat mechanism.

Pre-launch Acceptance Criteria

Before you confirm the integration is complete, check against the following list one by one:

Check ItemPass Criterion
SBE DecodingEvery frame can be parsed correctly to get seqId/prevSeqId (or tradeId), all field values fall within reasonable ranges
Sequence ContinuityAfter continuous operation for a period of time, the match rate between prevSeqId and the previous seqId meets expectations (excluding normal packet loss)
Gap HandlingManually simulate a gap (or capture a real gap), confirm the client discards the old book and re-subscribes, instead of trying to repair the gap
Post-reconnection StateStrategy stays paused after reconnection, and only resumes after the new snapshot is applied and the first increment sequence is continuous
HeartbeatConfirm the ping/pong mechanism works, silent disconnections can be detected and trigger reconnection

If you get stuck at the SBE decoding stage (cannot get the schema or frame layout), a practical fallback is to first get the order book building and disconnection recovery logic fully working with the JSON channel, and treat SBE as a later performance optimization. OKX's sequence mechanism and snapshot logic are identical across both encodings, the order book building and recovery logic you validated on JSON does not need to be rewritten when you switch to SBE, you only need to replace the decoding layer.

OKX Exchange
A leading global cryptocurrency platform,suitable for both beginners and experienced traders.
New user benefit: 20% off trading fees upon registration!!

References

  1. GitLab·QuanTech Services Group / Sytus Feed Connect Kit, page publish/update date: 2026-02-09; verification date: 2026-10-19.
  2. VoiceOfChain·OKX V5 WebSocket Orderbook Subscription Guide, page publish/update date: 2026-05-05; verification date: 2026-10-19.
  3. Decentralised News·Crypto WebSocket Reliability 2027, page publish/update date: 2026-09-27; verification date: 2026-10-19.
  4. OKX API (Node.js SDK)·Reconnection, page publish/update date: 2026-03-18; verification date: 2026-10-19.
  5. GitHub·okx-websocket-api-docs README, page publish/update date: no date marked on page; verification date: 2026-10-19.