Trade and diff_depth streams are not syncronized time to times

Hi, I’m developing local order book handling and syncronizing with trade stream.
I faced next problem: if I subscribe to websocket streams independently it causes situation like this:

[info] Trade received tid=12560 system_timestamp: 1700525763583 ms
Trade: [Timestamp: 1700525763166 ms, price: 37486.6, quantity: 0.00064, is_buyer_maker: 0]
[info] Depth received tid=12560 system_timestamp: 1700525763596 ms
OrderBook: [Timestamp: 1700525763389 ms
[info] Trade received tid=12560 system_timestamp: 1700525763852 ms
Trade: [Timestamp: 1700525763166 ms, price: 37486.8, quantity: 0.10571, is_buyer_maker: 0]

So as you see trade which is recieved after diff_depth has binance timestamp later than previous diff_depth. And trades which have same binance timestamp 1700525763166 recieved with kind of big gap and what’s interesting after depth update.

If I’m using combined stream (e.g. /stream?streams=btcusdt@depth@100ms/btcusdt@trade) then it works fine(binance timestamps recieved in ascending order which is OK).

Could you hint me for my case if it could be expected that independent websocket streams (notice that process thread id is the same so no thread race) are going in wrong sequence?
And shall I use combined streams for my case?

So basically I’m asking if there’s guarantee that if I subscribe on multiple websocket streams for same trade pair, then data will come in the correct order(by event time sequence) or in my case it’s better to subscribe on multiple streams at one websocket(e.g. /stream?streams=btcusdt@depth@100ms/btcusdt@trade) so binance can handle the correct order on its own side?

Thanks

Hello, as you might know it’s not 100% guaranteed that you’ll always receive websocket messages because packets can be lost during the TCP transmission if there’s an issue in the underlying network, which can be beyond server or client side’s control. So if you’re managing a local order book, I would suggest to follow this logic https://binance-docs.github.io/apidocs/spot/en/#how-to-manage-a-local-order-book-correctly, so that you’re able to overcome when the received Websocket message is no longer in sync.

Hi, thank you for your responce! Yeah I’ve implemented correct managin of order book. Now I want to add syncronized trades stream. So I want have like that:
Event | Timestamp
Trade 1000 ms
Trade 1003ms
Trade 1010ms
Depth update 1100 ms
Trade 1112 ms

But I have like:
Event | Timestamp
Trade 1000 ms
Depth update 1100 ms
Trade 1003ms
Trade 1010ms

So shall I connect to combined stream for my purpose so Binance can guarantee that packages will be sent in correct order? An why they could recieved by me in wrong sequence.
Thanks