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.
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 ofexpress-session
.Anyways, this should work for now with Express 4 and socket.io 1.1: