Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

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

Open
kgnadinger opened this issue Feb 2, 2018 · 14 comments

Comments

@kgnadinger
Copy link

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?

@craysiii
Copy link
Owner

craysiii commented Feb 3, 2018

Hey there,

Yes you're right. There is a keep-alive request you have to make via the REST API:

def keep_stream_alive(options)

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.

@kgnadinger
Copy link
Author

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.

@craysiii
Copy link
Owner

craysiii commented Feb 3, 2018

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.

@kgnadinger
Copy link
Author

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.

@craysiii
Copy link
Owner

craysiii commented Feb 3, 2018

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 craysiii changed the title Websocket closes after some time WebSocket documentation is lacking in regards to keep-alive and 24 hour stream limit Feb 3, 2018
@potter-the-cat
Copy link

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!

@craysiii
Copy link
Owner

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.

@kgnadinger
Copy link
Author

kgnadinger commented Apr 19, 2018

Editing comment

The problem has to do with my computers internal clock.

@dpblnt
Copy link

dpblnt commented Apr 25, 2018

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?

@thijsdekkers
Copy link

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

@craysiii
Copy link
Owner

craysiii commented Aug 9, 2018

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

@thijsdekkers
Copy link

@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
Copy link

@craysiii Any update?

@dpblnt
Copy link

dpblnt commented Aug 15, 2020

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

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants