1

Now.js quotes:

Simply pass a connect or express http server in nowjs.initialize and this.user.session should be available.

So:

express = require 'express'
app = module.exports = express.createServer()
connect = require 'connect'
nowjs = require 'now'
everyone = nowjs.initialize(app)

The output of this.user is:

{ 
  clientId: '353725111301231610',
  cookie: { 'connect.sid': 's0meC00k1e1nF0rm4ti0n' },
  session: undefined 
}

Any idea why session is undefined?

Patrick
  • 7,903
  • 11
  • 52
  • 87
  • You forgot to use the session middleware :) – Raynos Jan 30 '12 at 16:23
  • I found the now.js session support by ianserli with the middleware, but I'm talking about the native session support from Now.js 0.8.0. Still don't know how to get this to work. – Patrick Feb 07 '12 at 13:52

2 Answers2

4

I ran into this problem and turns out it was happening because I was initializing NowJS before configuring express.

app.configure(function(){
    ...

    app.use(express.cookieParser());
    app.use(express.session({ secret: "my_key", store: redisStore }));
    app.use(app.router);
});

var everyone = nowjs.initialize(app); //must be defined after app.configure

One thing to note is that session is not set until a request is made to the server so if the server is reset, the client socket will reconnect but session will be undefined until the browser does a page refresh.

jschr
  • 1,866
  • 2
  • 16
  • 21
  • I still can't get this to work. Anybody have a working example I can look at? Forced to use the sessionStore.get method instead. Does using a MemoryStore not work? – cdeutsch May 10 '12 at 23:13
0

I managed to find a solution (CoffeeScript), for every "everyone"-request:

self = this
sid = decodeURIComponent(this.user.cookie['connect.sid'])
sessionStore.get sid, (err, session) ->
    self.user.session = session
    <<more code here>>

Any way to get to this.user.session directly?

Patrick
  • 7,903
  • 11
  • 52
  • 87