Reputation: 1389
I'm using the express framework, and part of my web app uses socket.io to enable real-time chat. I need to get the current user's username, and create a room with him in it. The problem is I can't find a way to get the user info when socket.on('connection') is called. Here's the relevant code I have so far :
var express = require('express');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var mongoose = require('mongoose');
var session = require('express-session');
var http = require('http');
var routes = require('./routes/index');
var app = express();
var server = http.createServer(app);
var io = require('socket.io')(server);
server.listen(4000);
app.use(session({secret : 'anything',
saveUninitialized: true,
resave: true}));
app.use(passport.initialize());
app.use(passport.session());
mongoose.connect('mongodb://localhost/my_db');
var Schema = mongoose.Schema;
var UserDetail = new Schema({
username: String,
password: String
}, {
collection: 'users'
});
var UserDetails = mongoose.model('users', UserDetail);
app.use('/', routes);
app.use('/users', users);
app.post('/login',
passport.authenticate('local', {
successRedirect: '/loginSuccess',
failureRedirect: '/loginFailure',
})
);
io.on('connection', function(socket) {
console.log('socket stuff');
var sessionStore = session;
var sessionId = socket.handshake.sessionId;
sessionStore.get(sessionId, function(err, session) {
if(!err) {
console.log('no error');
if(session.passport.user) {
console.log('This is the users email address %s', session.passport.user);
}
}
});
socket.emit('newMessage', {content : 'Chat message content'});
});
The last function is where I'm having trouble. When a user requests a page, I can just find their username by req.user, but I have no idea how to do it when I don't have req. The code I have for sessions in io.on('connection') does not work at all, and just throws errors, figured I'd keep it in to show what I've tried so far.
Upvotes: 1
Views: 4519
Reputation: 5078
This has been asked a few times but the older answers I found do not work anymore. The answer lies in parsing socket.handshake.headers.cookie
but this is hacky and will depend on the internals of express-session
.
Anyways, this should work for now with Express 4 and socket.io 1.1:
var express = require('express'),
session = require('express-session'),
cookie = require('cookie'),
signature = require('cookie-signature');
var app = express(),
store = new session.MemoryStore(),
secret = 'session-secret-key',
name = 'connect.sid';
app.use(session({
name: name,
secret: secret,
store: store,
resave: true,
saveUninitialized: true
}));
var server = require('http').createServer(app),
io = require('socket.io')(server);
io.on('connection', function(socket) {
if (socket.handshake && socket.handshake.headers && socket.handshake.headers.cookie) {
var raw = cookie.parse(socket.handshake.headers.cookie)[name];
if (raw) {
// The cookie set by express-session begins with s: which indicates it
// is a signed cookie. Remove the two characters before unsigning.
socket.sessionId = signature.unsign(raw.slice(2), secret) || undefined;
}
}
if (socket.sessionId) {
store.get(socket.sessionId, function(err, session) {
console.log(session);
});
}
});
Upvotes: 1