node.bittrex.api
node.bittrex.api copied to clipboard
How do I close a websocket after I get the info I need.
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;
});
websocketClient.end()
?
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.
You'll have to hook onto the reconnecting
service handler and return true to disable the reconnect.
websocketClient.serviceHandlers.reconnecting = () => true
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?
Put it after
websocketClient = client;
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;
});
Maybe replace "true" with "false"...
websocketClient.serviceHandlers.reconnecting = () => false;
Still get the same result @vinnie035
Try that:
bittrex.options({ websockets: { autoReconnect: false } });
I have tried putting that in my options after my key and secret and that does not work either. @vinnie035
Calling end()
should close the connection...
https://github.com/mwwhited/signalr-client-nodejs/blob/master/WhitedUS.SignalR/WhitedUS.signalR/signalR.js#L393
@khuezy it does close the connect but it reconnects right away. And none of these methods to turn off autoReconnect have worked.
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.
I am testing this for the next release. There are issues with multiple disconnections/reconnections - once I resolve that it will go out.
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.