Avoiding/Detecting stale websocket (user data stream) connections


Currently, I experience some unexpected behavior in my websocket client for the Binance Spot API. The procedure looks like this:

  • fetch listenKey, establish websocket connection
  • periodically update listenKey (PUT) every 10 minutes (response: 200 OK, body: {})
  • make some trades
    -> user data received correctly
  • no active trading for some time (>> 1h) without any order placement/fills and thus no user data events
  • trades placed, fills happen
    -> no user data received any more, websocket connection seems open but dead

This happens sporadically.

The disconnects from Binance (as stated in docs, may happen every 24h) are registered correctly, so our used websocket implementation should not silently drop close frames or something alike. Since we detect “real” disconnects correctly and our connection stays open for longer periods of time, our used implementation seems to answer Binance’ ping frames correctly.

To be more explicit in my question:

  1. Is there any way to detect such a dead connection? Ping frames from my end seem to be answered correctly.
  2. Is it recommended to always reset the listenKey (DELETE and POST) if we want to reconnect?
  3. It seems that even invalid path/listenKey connections can be established, but won’t deliver any data whatsoever. Is there any way to detect this?

Many thanks in advance!