I'm trying to access the session object from an 'upgrade' event fired by a Node.js server using the Express.js framework. I have set up Session support correctly and can access it from the standard .get .post .put and .delete methods eg.
app.get('/', function(req, res) {
req.session.test = 'Hello world'; // Works fine
res.render('index');
});
But if I hook up an on 'upgrade' event on the server like so:
app.on('upgrade', function(req, socket) {
var message = req.session.test; // Doesn't work
});
I can't access the session object. As far as I'm aware, the Connect Session middleware only hooks up the session for the get/post/put/delete methods, but not for custom events such as 'upgrade'.
FYI, an 'upgrade' event is issued from a client during a WebSocket handshake, I'm trying to access the session from the initial WebSocket handshake. The session cookie is definitely there in the HTTP headers during the handshake eg:
app.on('upgrade', function(req, socket) {
var cookieHeader = req.headers['cookie'];
console.log(cookieHeader);
});
...will output the Connect session cookie. Is there perhaps a way to build up the session object manually using the raw session cookie token?
Update: Been speaking to to some people on the node.js IRC chatroom, apparently this is either very difficalt or impossible. The Connect middleware isn't exposed to custom/unusual events such as 'upgrade'. They said to raise an issue in Github, so here it is: