I use redis to store my sessions in expressjs. It works great when working locally and even works most of the time when deployed to a hero. The problem is that regularly (when deploying to the hero), I see that my session updates are lost.
eg. the user logs in to my website and I add their user object to the session:
req.session.user = user;
however (sometimes) when I try to restore an object after a few seconds (in another request), it does not exist
var currentUser = req.session.user;
I initialize the session store as follows
if (process.env.REDISTOGO_URL) {
console.log('Connecting to redis:' + process.env.REDISTOGO_URL);
var rtg = require('url').parse(process.env.REDISTOGO_URL);
var redis = require('redis').createClient(rtg.port, rtg.hostname);
redis.auth(rtg.auth.split(':')[1]);
} else {
console.log('Connecting to local redis');
var redis = require('redis').createClient();
}
app.use(express.cookieParser('secret key for cookie monster'));
var RedisStore = require('connect-redis')(express);
app.use(express.session({
store: new RedisStore({
client: redis
})
}));
Any ideas? Do I need to do any cleaning (or explicit storage) of session data?