Your own answer would probably suffice for this case, but you answered a more general question (how to access session info for a socket connection). The following is how I do it generally. Additionally, I normally register all the routes in such a way that they get a reference to a config, so I can also provide every request handler with a socket connection specific to this particular session:
var cookie = require("cookie"),
parseSignedCookie = require('connect').utils.parseSignedCookie;
var io = require('socket.io').listen(httpServer);
io.sockets.on("connection", function(socket) {
if (socket.handshake.headers.cookie) {
var cks = cookie.parse(socket.handshake.headers.cookie);
var sessionId = parseSignedCookie(cks["connect.sid"], config.sessionSecret);
config.sessionStore.get(sessionId, function(err, session) {
if (!err) {
console.info("connected socket for ID " + sessionId);
config[sessionId] = socket; // set socket to config so that request handlers could emit on it
socket.on("disconnect", function() {
console.info("disconnected socket for ID " + sessionId);
delete config[sessionId];
});
}
});
}
});
For the request handlers to know about the config I write them normally in the following form:
exports.get = function(conn, config) {
return function(req, res, next) {
var socket = config[request.sessionID];
if (socket) {
// socket.emit
}
res.end();
}
}
app.get("/", someModule.get(customConnection, config));
For the latter, see more under https://github.com/codelab-io/node-route-dir