socket.io ensure state on client reconnection

I am currently working on a project with socket.io, and i'm not sure to fully understand the mechanism of reconnection.

Since a disconnection could happen client side, i would like to know how to maintain the state of the socket on the server. I already know that socket.io-client will try to reconnect automatically, but i would like to know if it is possible to ensure the state of the socket on the server side.

I was thinking of a cookie based session, with express for example, but again i am not sure if i'm taking the good way about this. Is there another solution i should consider?

For the record, i successfully configured HAProxy with a cookie based sticky-sessions mechanism. Could it be possible to mix this mechanism with a cookie session on the socket.io server ?

Thanks

William

I think cookie based sessions are your best option. Look into the session.socket.io module. Looks like it was built specifically for this.

var SessionSockets = require('session.socket.io');
var sessionSockets = new SessionSockets(io, sessionStore, cookieParser);

sessionSockets.on('connection', function (err, socket, session) {

  //your regular socket.io code goes here 
  //and you can still use your io object

  session.foo = 'bar';
  //at this point the value is not yet saved into the session 

  session.save();
  //now you can read session.foo from your express routes or connect middlewares

});

Alternatively you could implement sessions yourself using express as you mentioned. I don't know of any easy way to integrate with HAProxy.