Reconnect implementation questions for C#

I use binance-connector-dotnet framework to connect to Coin-M Futures User Data Streams. I can get ACCOUNT_UPDATE and ORDER_TRADE_UPDATE which is what I need and everything works as expected.
Hovewer, I need to launch my app as a server which should work all the time. And here is a few technical questions about reconnection which I cannot find on the Forum:
As I understand from the topics on Forum the best approach to keep a live connection is to send ping and wait for the pong message - if no pong message in a short time frame (let’s say 5 sec), reconnect to the socket.

On the Framework’s GitHub Page stated:

Heartbeat. Once connected, the websocket server sends a ping frame every 3 minutes and requires a response pong frame back within a 10 minutes period. This package handles the pong responses automatically.

The problem is, I don’t see any implementation of the ping-pong in the Framework sources and wander does Framework really handle it automatically? Also, how can I send this ping message by myself? Looks like this is something obvious, but I cannot figure out how to send it with C#

Also, if I do reconnect to the Socket, do I need to request a new listenKey for that, or I keep using the same listenKey I periodically refresh (every 30 min)?

After investigation in ClientWebSocket class, which is used by binance-connector-dotnet Framework, I found that ClientWebSocket itself maintains the connectivity by KeepAliveInterval parameter, which is a time frame in which the ping-pong messages are sending to maintain Socket connection.

So my algorithm of reconnection now relays on 2 points:

  1. Call “/dapi/v1/listenKey” every 30 minutes to keep the listenKey active, as soon as I get the “listenKeyExpired” event from the Stream I close old connection, request new listenKey and open new connection.
  2. Read WebSocketState every 5 sec to determine if it is still Open - this is the only way I found because the ClientWebSocket class itself doesn’t have any OnOpen, OnError or OnClose events (at this point I regret I don’t know Python :), as here on the Forum a lots of questions and answers related to Python Framework). In case this State is not WebSocketState.Connecting or WebSocketState.Open - then I have to open new connection as well.

After testing of this approach I found that WebSocketState is always Open, even when I disable my laptop from Internet :frowning:

If you think there’s some issue with the library, please open a GitHub Issue in the repository with reproducible steps so we can have full context to explore further.

We’ll look into this and get back to you.

Thank you, it would be great if BinanceWebSocket class could have OnClose / OnError event (as Python Framework analog has), or perhaps Framework’s developers have another vision how to solve this problem and check the State of connection?

does Framework really handle it automatically?
I seems you’ve already investigated ClientWebSocket’s default KeepAliveInterval parameter.

I send this **ping** message by myself
There is PingSpotListenKey method, but I want to emphasize that while you can use some WebSocket functionalities with binance-connector-dotnet library, it is NOT written for Futures product (USD / COIN ), only for SPOT at the moment, which means you can’t lay in it for anything related with Futures product. We might include Futures in the future, but it’s not in the plan in the short term yet.

if I do reconnect to the Socket, do I need to request a new listenKey for that, or I keep using the same listenKey I periodically refresh (every 30 min)?
The logic for COIN futures’s listenKey validity for is available in the document.

Yes, I’m already implemented Futures Coin Service by analogy with Spot Service. Everything is working fine. The only problem I have, is that neither of BinanceWebSocket or BinanceWebSocketHandler has an OnError (OnClose) event, so I have to store IBinanceWebSocketHandler in variable and check it State in a async loop - this is the only way I found to handle socket disconnection caused by network (weak Internet connection for example). It would be convenient if BinanceWebSocket has this event, by analogy with OnMessageReceived callback.
I can open an issue on GitHub Repository, but it’s not an issue, it’s more like feature request :slight_smile:

If there’s something that you think the library can be improved, opening an open Issue on GitHub is the way to go, so that we can check and if feasible tag it as feature request :slight_smile:

You’re also more than welcome to open an PR! It’s the quickest way for the feature to be integrated.

1 Like