Arbuste Arbuste - 8 months ago 475
Node.js Question

Shared sessions (Express.js,, Passport.js)

I am trying to retrieve user data after a successful connection with Passport.js.

I use express-session and in order to share data. Here is a part of my code :

var expressSession = require('express-session');
var sharedsession = require("");
var MongoStore = require('connect-mongo')(expressSession);
var sessionStore = new MongoStore({
url: 'mongodb://',
ttl: 24 * 60 * 60 // = 1 day

var session = expressSession({
cookie: cookieParser,
secret: 'mySecret',
resave: false,
saveUninitialized: false,
store: sessionStore,


io.use(sharedsession(session, {
autosave: true

Here is where I try to save some data (req.session.test).

app.get( '/index.html', isLoggedIn, function( req, res ) {
req.session.test = 'Hello World !';
res.sendFile( root + '/index.html' );

I noticed that req.sessionID inside app.get() is different from client.request.sessionID inside io.on('connection, function(...)). This could be important according to the comments in this thread : How to share sessions with Socket.IO 1.x and Express 4.x?

I would be happy for any help ! :)


Actually, this was a CORS issue.

On client side, I connected to with :

var socket = io.connect(""); // with the server ip

This triggered cross origin requests, and limited the behaviour of passport.socketio ( Look at the "CORS-Workaround" section for more information.

Then, the solution was simply to follow the "workaround" :

var socket = io.connect('//' +;