node.bittrex.api icon indicating copy to clipboard operation
node.bittrex.api copied to clipboard

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

Open danschnettler opened this issue 7 years ago • 15 comments

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;
});

danschnettler avatar Dec 20 '17 09:12 danschnettler

websocketClient.end() ?

khuezy avatar Dec 20 '17 14:12 khuezy

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.

danschnettler avatar Dec 20 '17 22:12 danschnettler

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

websocketClient.serviceHandlers.reconnecting = () => true

khuezy avatar Dec 20 '17 23:12 khuezy

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?

danschnettler avatar Dec 21 '17 01:12 danschnettler

Put it after websocketClient = client;

khuezy avatar Dec 21 '17 02:12 khuezy

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;
});

danschnettler avatar Dec 21 '17 02:12 danschnettler

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

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

vinnie035 avatar Dec 21 '17 06:12 vinnie035

Still get the same result @vinnie035

danschnettler avatar Dec 21 '17 06:12 danschnettler

Try that:

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

vinnie035 avatar Dec 21 '17 08:12 vinnie035

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

danschnettler avatar Dec 21 '17 22:12 danschnettler

Calling end() should close the connection...

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

khuezy avatar Dec 21 '17 23:12 khuezy

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

danschnettler avatar Dec 21 '17 23:12 danschnettler

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.

TheRealest avatar Feb 09 '18 01:02 TheRealest

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

dparlevliet avatar Feb 17 '18 01:02 dparlevliet

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.

nengine avatar Feb 22 '18 04:02 nengine