I know this is a bit outdated, but for future readers, in addition to the approach described by @kentcdodds for parsing cookies and retrieving a session from storage (e.g. my own passport.socketio ), you can also consider a token-based approach.
In this example, I use JSON Web Tokens, which are pretty standard. You must point to the client page token, in this example, imagine the authentication endpoint that returns the JWT:
var jwt = require('jsonwebtoken'); // other requires app.post('/login', function (req, res) { // TODO: validate the actual user user var profile = { first_name: 'John', last_name: 'Doe', email: ' john@doe.com ', id: 123 }; // we are sending the profile in the token var token = jwt.sign(profile, jwtSecret, { expiresInMinutes: 60*5 }); res.json({token: token}); });
Now your socket.io server can be configured as follows:
var socketioJwt = require('socketio-jwt'); var sio = socketIo.listen(server); sio.set('authorization', socketioJwt.authorize({ secret: jwtSecret, handshake: true })); sio.sockets .on('connection', function (socket) { console.log(socket.handshake.decoded_token.email, 'has joined');
The malware socket.io-jwt expects a token in the query string, so you only need to connect it when connecting from the client:
var socket = io.connect('', { query: 'token=' + token });
I wrote a more detailed explanation of this method and cookies here .