binance icon indicating copy to clipboard operation
binance copied to clipboard

WebSocket documentation is lacking in regards to keep-alive and 24 hour stream limit

Open kgnadinger opened this issue 7 years ago • 14 comments

I have a websocket open pulling in price data and making buy/sell orders based on it.

After a few hours, the websocket will close with no indication why. Is there a keep alive message that needs to be sent to binance?

kgnadinger avatar Feb 02 '18 03:02 kgnadinger

Hey there,

Yes you're right. There is a keep-alive request you have to make via the REST API: https://github.com/craysiii/binance/blob/23533bb44bc2e4534950957fc1c7710c5db882f1/lib/binance/client/rest/account_api.rb#L176

It's recommended that you hit that endpoint every 30 minutes to keep your stream alive. You also have to realize that Binance has put a 24 hour limit on a WebSocket lifecycle, so you will have to reopen your WebSocket after that amount of time elapses. This should be more clear in the documentation, and I will update the README this weekend to give a better example of how to use the WebSocket in conjunction with EventMachine.

Sorry for the late reply.

craysiii avatar Feb 03 '18 16:02 craysiii

After a few 3:00am texts that it closed, I actually just re-opened the websocket in the close method and seems to work just fine.

kgnadinger avatar Feb 03 '18 16:02 kgnadinger

Nice. I worry about missing an update from the stream. Maybe it would be good to do a REST API call to get any information you might have missed in between the time the stream closed and reopened.

craysiii avatar Feb 03 '18 16:02 craysiii

Absolutely and I do that as well. I download relevant data in the open method. If the bot was down for 2 days I'd worry about missing some data.

kgnadinger avatar Feb 03 '18 16:02 kgnadinger

Awesome! Glad to hear you're making good use of the gem.

I'm going to go ahead and edit this issue to reflect the need to update the docs for newcomers. Thanks for pointing this out for me.

craysiii avatar Feb 03 '18 16:02 craysiii

Can you please provide some sample code as to how the code needs to look? Are you thinking something like this will work in the close?

close = proc { client.all_market_ticker(methods: methods) }

Thanks in advance!

potter-the-cat avatar Feb 16 '18 16:02 potter-the-cat

Hey @potter-the-cat

Yeah that should work fine. I am still working on updating the documentation on this. Got caught up in a metaprogramming swing but I should be back to doing normal maintenance this weekend.

craysiii avatar Feb 16 '18 17:02 craysiii

Editing comment

The problem has to do with my computers internal clock.

kgnadinger avatar Apr 19 '18 13:04 kgnadinger

Hi, i'm using the gem for a telegram notification bot amongst others, and I think I;m hitting one of the issues in this thread. My approach is to have a websocket.userdata on eventmachine, and a ruby thread for pinging binance, but I observe the bot disconnected, or missing events. I'm open to any hints, observations.

init

	@client = Binance::Client::REST.new( api_key: api_key, secret_key: secret_key )
	@ws    = Binance::Client::WebSocket.new

ping thread

Thread.new( @client ){ |client|
		loop do
			begin
				ret = client.ping
				sleep 60*30
			rescue => e
				p e.inspect
			end
		end
	}

eventmachine

Thread.new{
		begin
			EM.run do
				binance.ws.user_data( listen_key: binance.listen_key, methods: {message: binance.message} )
			end
		rescue =>err
				p err.inspect
		end
	}

Edit: I added listening to the 'open' and 'close' for ws.user_data(), and I noticed that there is no 'closed' event, but my bot is not receiving messages from binance after a while. 'closed' event gets called correctly when i close the bot. So I assume the websocket was open the whole time, but misses messages after a while. Might the Threads cause this in my case?

dpblnt avatar Apr 25 '18 06:04 dpblnt

Any update on this? Since this is essential I would really like to see some sample code.

thijsdekkers avatar Aug 08 '18 23:08 thijsdekkers

Hey thijsdekkers, I’ll have an update for you this weekend. Busy with work, sorry about that.

craysiii avatar Aug 09 '18 21:08 craysiii

@craysiii No problem at all, I appreciate the nice work you've already done. Just looking forward to the update so I can finally deploy my project! :)

thijsdekkers avatar Aug 13 '18 15:08 thijsdekkers

@craysiii Any update?

thijsdekkers avatar Aug 19 '18 22:08 thijsdekkers

client.keep_alive_stream!( listen_key: "the_key" ) results 404

... saved from url=(0032)https://www.binance.com/en/error ...

dpblnt avatar Aug 15 '20 16:08 dpblnt