n0mad01 / node.bittrex.api

No longer maintained
MIT License
183 stars 187 forks source link

Websocket reconnect? #25

Closed jaggedsoft closed 7 years ago

jaggedsoft commented 7 years ago

Thank you for this awesome library!! Is there a way to automatically reconnect the websocket in case it gets disconnected?

listen example

bittrex.websockets.listen( function( data ) {
  if (data.M === 'updateSummaryState') {
    data.A.forEach(function(data_for) {
      data_for.Deltas.forEach(function(marketsDelta) {
        console.log('Ticker Update for '+ marketsDelta.MarketName, marketsDelta);
      });
    });
  }
});

Currently when it gets disconnected all I see is "Connection aborted" in the console Thanks in advance! @dparlevliet @n0mad01

dparlevliet commented 7 years ago

this is my error - when I provided the example I wrote the comment incorrectly

      reconnecting: function (retry) {
        ((opts.verbose) ? console.log('Websocket Retrying: ', retry) : '');
        // change to false to stop retrying
        return true;
      }

should be


      reconnecting: function (retry) {
        ((opts.verbose) ? console.log('Websocket Retrying: ', retry) : '');
        // change to true to stop retrying
        return false;
      }
n0mad01 commented 7 years ago

i have pushed a fix, the default is set to reconnect the socket now as @dparlevliet suggests.

you also can redefine what has to happen on disconnected like this:

var websocketsclient = bittrex.websockets.listen( function( data ) { // etc ... });

websocketsclient.serviceHandlers.disconnected = function () { // do something ... };

these are all possible serviceHandlers:

bound: function() { console.log("Websocket bound"); },
connectFailed: function(error) { console.log("Websocket connectFailed: ", error); },
connected: function(connection) { console.log("Websocket connected"); },
disconnected: function() { console.log("Websocket disconnected"); },
onerror: function (error) { console.log("Websocket onerror: ", error); },
messageReceived: function (message) { console.log("Websocket messageReceived: ", message); return false; },
bindingError: function (error) { console.log("Websocket bindingError: ", error); },
connectionLost: function (error) { console.log("Connection Lost: ", error); },
reconnecting: function (retry /* { inital: true/false, count: 0} */) {
    console.log("Websocket Retrying: ", retry);
    //return retry.count >= 3; /* cancel retry true */
    return true;
}
jaggedsoft commented 7 years ago

You are both very awesome! Thank you for everything you have done. I am grateful!!

https://nextlocal.net https://nextlocal.me https://nextlocal.ca https://ravanaorganics.com https://buykratombulkusa.com