I'm using express.io...
In one of my routed events, I load a stored 'vote' from a database, then try to set that against the request's session:
app.io.route('ready', function(req){
if(req.session.username || req.session.twitter_user){
if(!req.session.username){ //makes the username to store in the session.
req.session.username = 't_' + req.session.twitter_user.screen_name;
}
winston.debug(req.session.username + ' connected'); //redders6600 connected.
db.getVote(req.session.username, function(err, vote){ //gets votes for the user.
if(!err){
req.session.vote = vote[0].vote;
req.io.emit('user data', {username: req.session.username, vote: req.session.vote});
//on the client, I receive this event, and it correctly contains username and vote
}
});
}
});
This all works okay, but when I try to access the session data from another routed event, req.session.vote is undefined. req.session.username gets set correctly.
I know that vote[0].vote is definitely not undefined, and it's verified when the client side code prints out the vote data from the 'user data' event, so I'm really quite confused as to what's happening. Originally I thought it must be something to do with the context of the callback function, but the fact that req.io.emit is working as expected makes me doubt that.
Not sure what else to try! Any help much appreciated....
I found this to be an issue with the express.io framework - I am not qualified enough to dig into the code to work out exactly why.
Instead I used a session sharing strategy similar to the one outlined here: http://howtonode.org/socket-io-auth
Which worked fine and wasn't a huge amount of hassle. As such I don't currently recommend working with express.io.