7

Is there a good way of using sessions with Socket.io in Express 3.0? A way of getting the clients' session id in a safe way? So that I can send notices to members specific to their account and make private chats from member to member?

I'm using MySQL store in Express 3.0

user2428118
  • 7,499
  • 4
  • 41
  • 69
georgesamper
  • 4,187
  • 5
  • 31
  • 58

3 Answers3

9

I wrote a tiny module to abstract it, here's how its usage looks like. It was written and tested using express 3, socket.io 0.9.10 and the (default) MemoryStore from connect 2.4.5. It should work fine with other compatible stores.

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

sessionSockets.on('connection', function (err, socket, session) {
  //your regular socket.io code goes here
});

For more details on how it works see https://github.com/wcamarao/session.socket.io

You might want to pay attention to the part of the README where it says how to use it with your own session store key (I'm assuming your mysql store uses a name other than the default 'connect.sid').

wcamarao
  • 234
  • 3
  • 8
1

You should check out express.io, a very simple micro-framework for express and socket.io integration. It handles express and socket.io sessions automatically.

npm install express.io

Check out the session support example here:

https://github.com/techpines/express.io/tree/master/examples#sessions

Brad C
  • 710
  • 8
  • 14
0

I did something slightly different to get it working. I read through a lot of posts on nodester github and came with the following solution....

Replace:

http.createServer(app).listen(app.get('port'), function(){
      console.log("Express server listening on port " + app.get('port'));
    });

with:

var server = http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

var io = require('socket.io').listen(server);

My plan is to continue with this workaround until issues around express3 and socket.io are resolved.

Mahesh
  • 118
  • 1
  • 12