Re: [Express-js] Sharing session with Socket.IO on different Node instances

452 views
Skip to first unread message

Brian Falk

unread,
May 30, 2013, 11:44:01 AM5/30/13
to expre...@googlegroups.com
Probably because you're using MemoryStore...  If you fire off two different processes they won't share the same memory and hence the socket.io process won't find the sessionId.  I would try using Redis (connect-redis) or Mongo (express-mongodb) as the session store.



On Thu, May 30, 2013 at 4:42 AM, Michael Dilloway <bko...@gmail.com> wrote:
I have put together a quick script where Express and Socket.IO are initialised at the same time. I set session variables in Express and am able to successfully retrieve them from connecting sockets.

var express = require('express'),
app = express();

var oSessionStore = new express.session.MemoryStore({reapInterval: 60000 * 10});
var sCookieSecret = '11ULQWZQCO';

app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.cookieParser(sCookieSecret));
app.use(express.session({store: oSessionStore}));
app.use(express.static(__dirname + '/public'));
app.use(express.logger('dev'));
});

require('./router')(app);
app.listen(8080);





var io = require('socket.io').listen(8081),
mysql = require('mysql');
io.set('log level', 1);

io.sockets.on('connection', function (socket) {
var oCookies = require('express/node_modules/cookie').parse(socket.handshake.headers.cookie);
var oSignedCookies = require('express/node_modules/connect/lib/utils').parseSignedCookies(oCookies, sCookieSecret);
var sSessionId = oSignedCookies['connect.sid'];

oSessionStore.get(sSessionId, function(err, session) {
console.log(err, session);
});
});


What I want is to have two separate scripts - One that initialises Express and one that initialises Socket.IO


var express = require('express'),
app = express();

var oSessionStore = new express.session.MemoryStore({reapInterval: 60000 * 10});
var sCookieSecret = '11ULQWZQCO';

app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.cookieParser(sCookieSecret));
app.use(express.session({store: oSessionStore}));
app.use(express.static(__dirname + '/public'));
app.use(express.logger('dev'));
});

require('./router')(app);
app.listen(8080);

... and ...


var io = require('socket.io').listen(8081),
mysql = require('mysql'),
express = require('express');
var oSessionStore = new express.session.MemoryStore({reapInterval: 60000 * 10});
var sCookieSecret = '11ULQWZQCO';
io.set('log level', 1);

io.sockets.on('connection', function (socket) {
var oCookies = require('express/node_modules/cookie').parse(socket.handshake.headers.cookie);
var oSignedCookies = require('express/node_modules/connect/lib/utils').parseSignedCookies(oCookies, sCookieSecret);
var sSessionId = oSignedCookies['connect.sid'];

oSessionStore.get(sSessionId, function(err, session) {
console.log(err, session);
});
});


Suddenly Socket.IO fails to read the session and I have no idea why! Any ideas on what I'm doing wrong?

--
You received this message because you are subscribed to the Google Groups "Express" group.
To unsubscribe from this group and stop receiving emails from it, send an email to express-js+...@googlegroups.com.
To post to this group, send email to expre...@googlegroups.com.
Visit this group at http://groups.google.com/group/express-js?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Dilloway

unread,
Jun 4, 2013, 6:15:26 AM6/4/13
to expre...@googlegroups.com
That makes sense! Thanks for the response
Reply all
Reply to author
Forward
0 new messages