question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to keep connection alive?

See original GitHub issue

Hi,

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:closed
  • Created 2 years ago
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ZigmundLcommented, May 16, 2021

var reqResult = restClient.Spot.UserStream.KeepAliveUserStream(ListenKey); It has to be sent at least once an hour, binance recommends to do it more often.

0reactions
Smileynatorcommented, Jun 20, 2021

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found