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

Connection aborted and no reconnect attempt ?

Open thechile opened this issue 8 years ago • 17 comments

I get this error quite frequently: Connection aborted

It triggers the serviceHandlers.disconnected handler but the error is undefined.

Is there a correct way to trigger a reconnect manually with the disconnected handler ?

Using latest npm version. Any ideas ?

Many thanks

thechile avatar Jul 28 '17 11:07 thechile

an automatic reconnection try should already be initiated, the return false on the reconnecting serviceHandler would do that, but if you need to reset it try the following:

websocketsclient.serviceHandlers.reconnecting = function (retry) {
  return false;
}

for manual restarts it occurs to me that following serviceHandler methods seem more appropriate:

websocketsclient.serviceHandlers.connectionLost: function (error) {}
websocketsclient.serviceHandlers.disconnected: function () {}

n0mad01 avatar Jul 29 '17 10:07 n0mad01

It seems to run for some amount of time, several hours and then abruptly disconnects with no error and it doesn't appear to try and reconnect. I will add in serviceHandlers.reconnecting and see if that is triggered.

Is there a way to ask the current websocket sessions to be disconnected ? So if disconnected is triggered, i can just call my init function that calls websockets.subscribe again to start everything up again.

thechile avatar Aug 01 '17 03:08 thechile

Yes, I have experienced the same thing not just with Bittrex but Poloniex, too. As described here https://github.com/websockets/ws#how-to-detect-and-close-broken-connections it's possible for a connection to be broken without the client or server knowing about it. The reasons may vary. Personally I have my script forcing disconnect/reconnect every 30 minutes.

var client;
const connect = () =>
  client = new signalR.client(
              "wss://socket.bittrex.com/signalr",
              ['CoreHub']
            );
};
connect();
setInterval(() => {
  client.end(); // force close the connection
  connect();
}, 30*60*1000);

For Poloniex I have this at 5 minutes because I found it far more common there.

dparlevliet avatar Aug 01 '17 05:08 dparlevliet

ok thanks.. will try that because i just had it again.

Here is rough transcript.. ran for 3 hours fine, then just died.

Websocket bound
Websocket connected
Subscribed to BTC-ETH
Subscribed to BTC-SC
Subscribed to BTC-XRP
Subscribed to BTC-ZEN
....
Websocket disconnected
Connection aborted

Even though i have verbose' : true and the below code, i saw no attempt to reconnect. Just stops.

    bittrex_ws.serviceHandlers.reconnecting = function (retry) {
        console.log("reconnecting...");
        return false;
    }

thechile avatar Aug 01 '17 07:08 thechile

in your example, shouldn't it be setInterval ?

thechile avatar Aug 01 '17 07:08 thechile

gave up on Poloniex because there orderbook feed doesn't include any data. Only the ticker feed works.

thechile avatar Aug 01 '17 07:08 thechile

it should, yes, sorry.

dparlevliet avatar Aug 01 '17 08:08 dparlevliet

If you're having the disconnected event being triggered then my example doesn't apply. My example was for the cases I discovered where it was disconnected but it didn't know about it so it never retried. I assume you're using the latest version?

dparlevliet avatar Aug 01 '17 08:08 dparlevliet

yea latest version. Is there a debug mode i can enable to give more verbose reason behind the aborted connection ? The connection aborted message doesn't seem to be from this library.. i suppose it's from signalR.

thechile avatar Aug 01 '17 12:08 thechile

The library does very little in the middle. You're basically attached directly to signalR and the library just provides a couple of wrapper commands to make it simpler for you to get connected. You could try look at the data sent to the disconnect function.

bittrex_ws.serviceHandlers.disconnected = function () {
  console.log(arguments);
}

I will setup my own test and see if I can replicate it.

dparlevliet avatar Aug 01 '17 13:08 dparlevliet

I have noticed this as well, seems to run fine for some time, for me usually some hours or 1-2 days, and then it just disconnects without trying to reconnect. Strange thing is, it actually does reconnect most of the time, but sometimes it just stops. Is there a way to manually reset the websocket connection of the api wrapper? I know it works for the base client as dparlevliet pointed out, could someone show how to reset the client of the wrapper?

HonoldMatoma avatar Aug 07 '17 09:08 HonoldMatoma

Alright never mind, just using the listen function again seems to work. In case anyone is interested on how to determine if the client needs to reconnect, my application compares the nounce to the last nounce 15 seconds ago. If it didn't change, my application reconnects.

HonoldMatoma avatar Aug 12 '17 14:08 HonoldMatoma

I got same issue. It seems, web socket was disconnected. in console.log there is 'connection aborted'. so I wrote this code

websocketsclient.serviceHandlers.reconnecting = function (retry) {
	console.log('reconnecting');
	return false;
}

but it is not working still. How can I reconnect automatically when connection was aborted? who can help me? Thanks in advance

webdeveloper0429 avatar Aug 18 '17 00:08 webdeveloper0429

This should do it:

bittrexSocket.serviceHandlers.disconnected = (websocket) => {
  console.log("bittrex disconnected, reconnecting immediately");
  setImmediate(() => {
    bittrexSocket.start();
  });
}

cluttered-code avatar Aug 23 '17 07:08 cluttered-code

Is there any solution yet? i tried @cluttered-code but that just crash my code, and in my IDE shows errors.

gerardo15 avatar Sep 06 '17 17:09 gerardo15

@gerardo15 are you using es2015(es6)?

cluttered-code avatar Sep 07 '17 01:09 cluttered-code

@gerardo15 Try the older format if you aren't using es2015.

es2015 (es6)

const bittrex = require('node.bittrex.api');

const bittrexSocket = bittrex.websockets.listen((data) => {
  // Do Something
});

// Reconnect when disconnected
bittrexSocket.serviceHandlers.disconnected = (websocket) => {
  console.log("bittrex disconnected");
  setImmediate(() => {
    bittrexSocket.start();
  });
};

es5

var bittrex = require('node.bittrex.api');

var bittrexSocket = bittrex.websockets.listen(function (data) {
  // Do Something
});


// Reconnect when disconnected
bittrexSocket.serviceHandlers.disconnected = function (websocket) {
  console.log("bittrex disconnected");
  setImmediate(function () {
    bittrexSocket.start();
  });
};

cluttered-code avatar Sep 07 '17 01:09 cluttered-code