craysiii / binance

API Wrapper for the Binance cryptocurrency exchange written in Ruby.
MIT License
97 stars 79 forks source link

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

Open kgnadinger opened 6 years ago

kgnadinger commented 6 years ago

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 commented 6 years ago

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.

kgnadinger commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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.

potter-the-cat commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

Editing comment

The problem has to do with my computers internal clock.

dpblnt commented 6 years ago

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 commented 6 years ago

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

craysiii commented 6 years ago

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

thijsdekkers commented 6 years ago

@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 commented 6 years ago

@craysiii Any update?

dpblnt commented 4 years ago

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

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