Skip to content
This repository has been archived by the owner on May 1, 2018. It is now read-only.

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

Open
danschnettler opened this issue Dec 20, 2017 · 15 comments
Open

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

danschnettler opened this issue Dec 20, 2017 · 15 comments

Comments

@danschnettler
Copy link

danschnettler commented Dec 20, 2017

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

khuezy commented Dec 20, 2017

websocketClient.end() ?

@danschnettler
Copy link
Author

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

khuezy commented Dec 20, 2017

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

websocketClient.serviceHandlers.reconnecting = () => true

@danschnettler
Copy link
Author

danschnettler commented Dec 21, 2017

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

khuezy commented Dec 21, 2017

Put it after
websocketClient = client;

@danschnettler
Copy link
Author

danschnettler commented Dec 21, 2017

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

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

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

@danschnettler
Copy link
Author

Still get the same result @vinnie035

@vinnie035
Copy link

Try that:

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

@danschnettler
Copy link
Author

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

@khuezy
Copy link

khuezy commented Dec 21, 2017

@danschnettler
Copy link
Author

danschnettler commented Dec 21, 2017

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

@TheRealest
Copy link

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

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

@nengine
Copy link

nengine commented Feb 22, 2018

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.

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

No branches or pull requests

6 participants