I am using node.js and expressing socket.io. I am using a session in Express.
How can I read a session and work with it in socket.io - in the part with communication?
store.userid undefined.
var express = require('express') , stylus = require('stylus') , nib = require('nib') , sio = require('socket.io') , ejs = require('ejs'); store = new express.session.MemoryStore; app.configure(function () { app.use(express.bodyParser()); app.use(express.cookieParser()); app.use(express.session({ secret: 'secret', store: store })) app.use(stylus.middleware({ src: __dirname + '/public', compile: compile })) app.use(express.static(__dirname + '/public')); app.set('views', __dirname); app.set('view engine', 'ejs'); //disable layout app.set("view options", {layout: false}); }); app.get('/', function(req, res) { req.session.userid = Math.floor(Math.random()*5000); }); var io = sio.listen(app) , nicknames = {}; io.configure(function () { io.set('transports', ['websocket','flashsocket','xhr-polling']); }); io.sockets.on('connection', function (socket) { socket.emit('hello', { hello: store.userid }); //store.userid is undefined });
In the variable store:
store =
{ sessions: { 'DNHIsZqgk53zbK3xg8qescJK.dUbdgfVq0D70UaLTMGTzO4yx5vVJral2zIhVsfFGnBA': '{"lastAccess":1326317111111,"cookie":{"originalMaxAge":14399999,"expires":"2012-01-12T01:28:17.266Z", "httpOnly":true,"path":"/"},"userid":3528}' }, hash: [Function], generate: [Function] }
Jenan source share