Socket.io Express 3 sessions

express, javascript, node.js, socket.io

Solution

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').

Problem

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

Original source

Related problems