2

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

William Poussier
  • 1,328
  • 3
  • 14
  • 27
  • What state information needs to persist? – gregnr Mar 25 '15 at 01:34
  • The rooms list of the socket, and a userId which come from a mongo database. – William Poussier Mar 25 '15 at 01:45
  • That information is already stored server-side with each socket and is accessible at any time. It still isn't apparent what problem you're actually trying to solve. – jfriend00 Mar 25 '15 at 01:45
  • Yes this room list is stored, but unfortunately, in case of disconnection, socket.io clear all the socket room before firing the `disconnect` event. This is one reason of storing this rooms list. – William Poussier Mar 25 '15 at 01:51
  • A temporary client disconnect and reconnect should retain the same `socket` state on the server (rooms, ids, etc...). A reconnect that doesn't work on the client should go through it's normal state of setting up a new socket which should put it in the rooms that you would normally put that client in just like it was connecting for the first time. The `socket.id` will not be permanent if there's a longer disconnection. You will have to use a cookie or some other session-based info to make a more permanent reference for that client - that is not a feature that socket.io offers built-in. – jfriend00 Mar 25 '15 at 02:10
  • You really ought to clarify your question as to what exactly you're asking about. It was not clear to me at all what your question was asking about as I wrote an answer about something different until others tried to tell me what they think your question means. Please offer a specific scenario that happens to a client and exactly what you want to have happen on the server. – jfriend00 Mar 25 '15 at 02:12

1 Answers1

0

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.

gregnr
  • 1,184
  • 7
  • 11
  • Hey! Do you think it could work with a non-browser client, let say node client ? – William Poussier Mar 25 '15 at 02:04
  • Not without some work. Cookies are passed through headers in the HTTP handshake and Node's socket.io-client doesn't seem to support modifying these headers out of the box. If you really need a non-browser client, your easiest solution may just be to listen for an event from client upon connection that will pass the session information. Alternatively you could pass it through query strings. See [this answer](http://stackoverflow.com/questions/13745519/). – gregnr Mar 25 '15 at 02:27