How to detect listenKey expiration on userDataStream websocket for Spot?

I’m using this endpoint:
wss://stream.binance.com:9443/ws/[listenKey] for userDataStream updates

There is no listenKeyExpired event on userDataStream websocket for Spot.
And pings are going forward and back successfully for websocket with expired listenKey.
So how to detect listenKey expiration?
I need to reconnect to websocket server with new listenKey in that case to continue receive updates.

Requesting a new listenKey after the current one gets expired doesn’t seem to be a good idea since you might lose an event update during the no active listenKey time, even if it’s short. The best is to not let the listenKey expire.

When you say “pings are going forward and back successfully for websocket with expired listenKey.”, do you mean you’re doing PUT /api/v3/userDataStream to a closed listenKey and still being successful? I think you should’ve received an error code `-1125.

Our system do POST /api/v3/userDataStream every 30 min to refresh current listenKey.
And our system do send Websocket ping and reconnects if no pong is received in 5 seconds.

The problem part is when other 3rd-party also uses API key for user’s account and do DELETE /api/v3/userDataStream. And current stream become closed and silent, with no disconnection or ping-pong fail on websocket side.
I can’t find the solution how to detect this in real-time.

Futures API sends “listenKeyExpired” event to detect it but Spot API don’t.

I see, unfortunately there’s no similar to “listenKeyExpired” event in Futures on Spot, the reason is because keeping alive the “listenKey” within every 60min would be enough. In your case, can you test by having 2 different API Keys to have two different listenKeys, so that the other party doesn’t affect your side?

Thanks for your responses.
We’ve already tested using 2 different API keys for one account.
Unfortunately, all API keys share the same “listenKey” i.e. POST /api/v3/userDataStream returns one value for different API keys.
Therefore DELETE /api/v3/userDataStream disables listenKey for all API keys.

any solution for this problem ?

I’m pretty sure same thing happening on futures. Because some keys in my app connected to futures stream receiving listenKeyExpired almost instantly. Like 1 or 2 minutes after creation. Binance really need to invest more time to fix issues with streams

As far as I can get, on futures we’re suffering from random key expiration, we can loose an event and no stable workaround found so far?

listenKeyExpired event is now available on spot testnet:

Changelog

2024-03-13

User Data Stream
  • A new event listenKeyExpired is now emitted when a listenKey expires.

Documentation: Listen Key Expired

1 Like