How to keep connection alive?
See original GitHub issueHi,
I have socket subscriptions for spot and margin trades and anytime for example I buy or sell a new coin I add or remove it from my app gridview control’s list so that for example when I wake up I can see I have coin X or Y and see their PNL.
But a lot of times the socket connection seems to be lost so that I don’t get any of these updates. I have to restart the app and then my app will detect if any new coin is in my wallet but not on the logs.
JKorf said I have to use keepAlive function but I couldn’t find this anywhere using the object browser.
Any help is appreciated!
` var spotListenKey = client.Spot.UserStream.StartUserStreamAsync ( ); var marginListenKey = client.Margin.UserStream.StartUserStreamAsync ( );
// Subscribe to a user stream | SPOT account
socketClient.Spot.SubscribeToUserDataUpdates ( spotListenKey.Result.Data,
// Order update
data =>
{
// Order update
string symbol = Utility.GetBaseSymbol ( data.Symbol );
if ( data.Side == OrderSide.Buy )
Utility.UpdateActions.Enqueue ( new UpdateActionType ( UpdateType.BuyOrder, data ) );
else if ( data.Side == OrderSide.Sell )
Utility.UpdateActions.Enqueue ( new UpdateActionType ( UpdateType.SellOrder, data ) );
},
// OCO updates
null,
// Position updates
null,
// Balance updates
data =>
{
Utility.UpdateActions.Enqueue ( new UpdateActionType ( UpdateType.BalanceUpdate, data ) );
}
);
// Subscribe to a user stream | MARGIN account
socketClient.Spot.SubscribeToUserDataUpdates ( marginListenKey.Result.Data,
// Order update
data =>
{
string symbol = Utility.GetBaseSymbol ( data.Symbol );
if ( data.Side == OrderSide.Buy )
Utility.UpdateActions.Enqueue ( new UpdateActionType ( UpdateType.BuyOrder, data ) );
else if ( data.Side == OrderSide.Sell )
Utility.UpdateActions.Enqueue ( new UpdateActionType ( UpdateType.SellOrder, data ) );
},
// OCO updates
null,
// Position updates
null,
// Balance updates
data =>
{
Utility.UpdateActions.Enqueue ( new UpdateActionType ( UpdateType.BalanceUpdate, data ) );
}
);`
Issue Analytics
- State:
- Created 2 years ago
- Comments:16 (5 by maintainers)
Top Results From Across the Web
Improving Website Performance: Enabling Keep-Alive
To enable Keep-Alive, you need to explicitly request it via the HTTP header by accessing .htaccess or the main configuration file of your...
Read more >What is HTTP Keep Alive | Benefits of Connection ...
The HTTP keep-alive header maintains a connection between a client and your server, reducing the time needed to serve files. A persistent connection...
Read more >sockets - Explain http keep-alive mechanism
A keep-alive allows an existing TCP connection to be re-used for multiple requests/responses, thus avoiding all of that overhead. That is what ...
Read more >Keep-Alive - HTTP - MDN Web Docs
The Keep-Alive general header allows the sender to hint about how the connection may be used to set a timeout and a maximum...
Read more >2. TCP keepalive overview
The most common and logical policy is to keep newest connections and to discard old and inactive connections first. Returning to Peers A...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
var reqResult = restClient.Spot.UserStream.KeepAliveUserStream(ListenKey); It has to be sent at least once an hour, binance recommends to do it more often.
Sorry to necro this, but i saw in another part of the documentation the following: https://binance-docs.github.io/apidocs/spot/en/#user-data-streams
A single connection to stream.binance.com is only valid for 24 hours; expect to be disconnected at the 24 hour mark
Meaning that after 24 hours, you are most likely going to need a reconnect anyway? Your work around of 10 seconds no order update is what i am probably going to apply as well. But it is less than ideal that the connection just dies without warnings.
As an added thing. What is the best way to update yourself after such a disconnect? Use the rest API to get “My Trades” or if you are really punctual “All Orders” using your lowest unresolved trade/order’s unique ID?