dparlevliet / node.bittrex.api

Node Bittrex API is an asynchronous node.js library for the Bittrex API, the data can be received either via GET request or Stream.
MIT License
253 stars 100 forks source link

How do I close a websocket after I get the info I need. #97

Open danschnettler opened 6 years ago

danschnettler commented 6 years ago

I don't see any documentation on how to close the websockets. Is there a way to implement code that would look like this?

var count = 0;
bittrex.options({
    websockets: {
        onConnect: function() {
            console.log('Websocket connected');
            bittrex.websockets.listen(function(data, client) {
                if (data.M === 'updateSummaryState') {
                    data.A.forEach(function(data_for) {
                        data_for.Deltas.forEach(function(marketsDelta) {
                             console.log('Ticker Update for '+ marketsDelta.MarketName, marketsDelta);
                             count++;
                             if(count > 4){
                                 //CLOSE WEBSOCKET
                             }
                        }
                    )}
                }
            })
        }                    
        onDisconnect: function() {
            console.log('Websocket disconnected');
        }
    }
});

var websocketClient;
bittrex.websockets.client(function(client) {
  websocketClient = client;
});
khuezy commented 6 years ago

websocketClient.end() ?

danschnettler commented 6 years ago

I just tried this and it does trigger the onDisconnect. However after that the websocket connects again. So it looks like this:

Websocket connected
<Ticker Data>
Websocket disconnected
Connection aborted
Websocket connected
<Ticker Data>
Websocket disconnected
Connection aborted

and it continues to do this.

khuezy commented 6 years ago

You'll have to hook onto the reconnecting service handler and return true to disable the reconnect.

websocketClient.serviceHandlers.reconnecting = () => true

danschnettler commented 6 years ago

I'm sorry I'm pretty new to this but I don't know what you mean by "hook onto". Do I just need to put that line of code somewhere or do more than that?

khuezy commented 6 years ago

Put it after websocketClient = client;

danschnettler commented 6 years ago

Thank you for your help on this but I'm still getting the same result with this:

var websocketClient;
bittrex.websockets.client(function(client) {
  websocketClient = client;
  websocketClient.serviceHandlers.reconnecting = () => true;
});
vinnie035 commented 6 years ago

Maybe replace "true" with "false"...

websocketClient.serviceHandlers.reconnecting = () => false;

danschnettler commented 6 years ago

Still get the same result @vinnie035

vinnie035 commented 6 years ago

Try that:

bittrex.options({ websockets: { autoReconnect: false } });

danschnettler commented 6 years ago

I have tried putting that in my options after my key and secret and that does not work either. @vinnie035

khuezy commented 6 years ago

Calling end() should close the connection...

https://github.com/mwwhited/signalr-client-nodejs/blob/master/WhitedUS.SignalR/WhitedUS.signalR/signalR.js#L393

danschnettler commented 6 years ago

@khuezy it does close the connect but it reconnects right away. And none of these methods to turn off autoReconnect have worked.

TheRealest commented 6 years ago

I know this is old but the key is disabling the autoReconnect option (which is enabled by default) and then calling .end() on the signalR client -- this is what I've done (in coffeescript but you get the idea):

endWebsocket = (cb) ->
  options.websockets.autoReconnect = false
  bittrex.options options

  bittrex.websockets.client (wsClient) ->
    wsClient.end()
    setImmediate cb

@danschnettler: if you never want to use autoReconnect that's fine, you can disable from the start when you set your keys like you were saying. Just make sure it's set as options.websockets.autoReconnect, not options.autoReconnect. Then when you want to kill the connection you just call bittrex.websockets.client to get the client and then client.end() to end it.

If you're still having trouble feel free to post the code so we can take a look.

dparlevliet commented 6 years ago

I am testing this for the next release. There are issues with multiple disconnections/reconnections - once I resolve that it will go out.

nengine commented 6 years ago

Sorry not directly related to this problem, but I like to log when websocket is trying to reconnect after it has been disconnected. Any suggestion is appreciated.