Flotype / now

NowJS makes it easy to build real-time web apps using JavaScript
http://www.nowjs.com
MIT License
1.92k stars 175 forks source link

nowjs.sessions is empty #184

Open ghost opened 12 years ago

ghost commented 12 years ago

I'm using Express sessions, and can read session data from req.session in the Express application object. However, I'm not able to read data from this.user.session because nowjs.sessions is never populated. The callback function in this block of code in now.js (starting line 210) never seems to get invoked:

if('use' in server && 'stack' in server && 'route' in server) {
    server.use(function(req, res, next) {
       self.sessions[req.sessionID] = req.session;
       next();
    });
}

See issue #175 which may be related / identical. Thanks.

ghost commented 12 years ago

Using v0.8.1

Can anyone confirm this issue, or am I doing something wrong? Thanks.

lukifer commented 12 years ago

I'm experiencing this as well. If I figure out a fix, I'll post it here. I'm suspecting something to do with the order in which middleware is declared.

lukifer commented 12 years ago

I haven't identified the real root cause, but I've isolated the issue to static routing:

app.use(express.static(__dirname + '/public'));

This seemed to prevent session functionality regardless of when it was loaded. I was able to sidestep the issue by using routes instead and disabling the static handler. I'd love to have a better fix, though.

ghost commented 12 years ago

lukifer, thanks for looking at this. For me, disabling static routing has no effect -- nowjs.sessions and this.user.session remain empty. Below is the block of code I use to create the Express server. I've tried disabling static routing, methodOverride(), and responseTime() but none of these had any effect.

var express = require('express'); var app = module.exports = express.createServer(); app.configure(function() { app.set('views', dirname + '/views'); app.set('view engine', 'eco'); app.set('view options', {layout: false}); app.use(express.bodyParser({ uploadDir: '/tmp/node', keepExtensions: false })); app.use(express.methodOverride()); app.use(express.cookieParser()); app.use(express.session({ secret: '****', store: sessionStore })); app.use(express.responseTime()); app.use(express.static(dirname + '/public')); app.use(app.router); });

var nowjs = require('now'); var everyone = nowjs.initialize(app, { cookieKey: 'connect.sid' });

wired8 commented 12 years ago

Here is a fix which works for me:

Setup your sessionstore:

// use redis as session middleware sessionStore = new RedisStore({'db':'1', maxAge: 1209600000});

nowjs.sessions(sessionStore); nowjs.sessionStore = sessionStore;

Modify the user now user module:

now/lib/user.js

Replace this:

// Populate session by parsing cookie var cookie = this.user.cookie[nowjs.options['cookieKey']]; if (cookie) { this.user.session = nowjs.sessions[unescape(cookie)]; }

With this:

var sid = decodeURIComponent(this.user.cookie['connect.sid']); if (sid) { var self = this; nowjs.sessionStore.get(sid, function (err, session) { self.user.session = session; return; }); }

You should now have session info in your user object.