Ws: Reconnecting a client

Created on 5 Mar 2013  路  5Comments  路  Source: websockets/ws

Hi,

I'm having issues with reconnecting a client - not really sure what's going on. When there's an error or close event, I do a websocket_client = null, and then instantiate a new client. This works, except the event listeners start to double up - so with each disconnect and subsequent reconnect, additional methods are added to the callbacks. So this obviously compounds, and when the service comes back up, there are a ton of connects and reconnects really quickly, destabilizing everything.

Looking through the code, I now try:

wsc.terminate();
wsc = null;

wsc = new WebSocketClient(...)

However, on disconnect, it will attempt one reconnection, and then throw an error:

ERROR:14:57:18:lib/websocket/client:on_error - Client "..." to "ws://...:35110", error: "ECONNREFUSED"
  DEBUG:14:57:18:lib/websocket/client:error - Client "..." to "ws://...:35110", retry in 1000 seconds

events.js:71
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: connect ECONNREFUSED
    at errnoException (net.js:770:11)
    at Object.afterConnect [as oncomplete] (net.js:761:19)

You can see on the next retry, after a second, it dies because there's no event listener attached (removeAllListeners is called), but I'm not quite understanding why. It's quite likely I'm doing this wrong. Any help would be appreciated.

Most helpful comment

@CarloAl: 3+ years later, what's the current way to have a socket automatically reconnect using socket.io?

@3rd-Eden: Primus and socket.io seem overkill for the simple task of reconnecting to a websocket. What are the current thoughts on providing that functionality in ws? Other modules trying to do this, don't always succeed.

All 5 comments

I think I've got this solved, but I can see what's breaking it. My "base class" is also an EventEmitter. When I do this:

this.ws.onerror = function(err) {
    self.on_error.apply(self, arguments);
};

It runs fine. Now when I do this:

this.ws.onerror = function(err) {
    self.emit('error', err);
    self.on_error.apply(self, arguments);
};

It will go with the above behavior and break. I know I'm missing something obvious...

So any events emitted out of error cause all of the events to be removed, even though I've instantiated a new EventEmitter that is in the local scope.

Sorry to bring back this old issue, but I was wondering if in the meantime something similar to socketIO reconnect has been implemented.

This is out of the scope of the project. If you want reconnect, implement something like that on top of WebSockets by reopening the connection after it has been closed.

If you don't want to implement this your self I would suggest looking at libraries that already does this for you like primus does.

@CarloAl: 3+ years later, what's the current way to have a socket automatically reconnect using socket.io?

@3rd-Eden: Primus and socket.io seem overkill for the simple task of reconnecting to a websocket. What are the current thoughts on providing that functionality in ws? Other modules trying to do this, don't always succeed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pmcalabrese picture pmcalabrese  路  4Comments

binarykitchen picture binarykitchen  路  5Comments

HanHtoonAung picture HanHtoonAung  路  3Comments

cra0kalo picture cra0kalo  路  3Comments

cmnstmntmn picture cmnstmntmn  路  4Comments