1

I'm trying to build in a reconnection procedure for clients that lose connection. In the client js I can detect when connection is lost and periodically attempt to reconnect to the server. When I reconnect though the server will be using a new socket.

On the client this is my reconnect logic

var socket = io.connect(ip, {
  'reconnect': true, //will create a new socket on the server
  'reconnection delay': options.retryInterval,
  'max reconnection attempts': 10
});

socket.on("disconnect", function() {
   this.connected = false;
   this.autoretry();
});

//establish the connection - similiar to the reconnect method
socket.emit("connect", this.options);

//....
autoretry: function() {
    if(this.connected) {return;};
    var next = this.__retry *= this.options.retryScalar;
    this.socket.emit("retry", this.connect.options);
    return _.delay(this.autoretry, next, this);
}

The problem is each client has it's own tls connection to an external server and I would like to hold this connection open for a brief period; waiting to see if the client reconnects.

Is there a good way to uniquely identify the client in node or should I create the identifier on the client and pass it to the server?

megawac
  • 10,953
  • 5
  • 40
  • 61

1 Answers1

1

For identifying clients you,should make socket.io work with expressjs Sessions, each session has a unique id, and uses many additional methods to identify itself to server-side, (cookies,headers..)

http://howtonode.org/socket-io-auth

also sockets have a unique id by default when the socket object is passed to callback io.on('connection',function(socket) , access by socket.id property

For identifying a client that lost connection, and script tries to reconnect, try listening for reconnect,reconnecting,reconnect_failed events.

https://github.com/LearnBoost/socket.io/wiki/Exposed-events

Gntem
  • 6,949
  • 2
  • 35
  • 48
  • Is there a way to identify a reconnecting on the server because its still creating a new socket with a different `id` – megawac Nov 03 '13 at 04:18
  • Found my answer: http://stackoverflow.com/questions/9668996/does-socket-io-reconnects-re-run-connect going to make a unique client id on the browser. – megawac Nov 03 '13 at 04:25