I am using node.js with socket.io and redisStore for my express-sessions. on my socket handshake, i want to read the cookie to get the session cookie, so i am able to read from and write to the session. here is what i got so far:
in my app.js:
var express = require('express');
var app = express();
var redis = require('redis');
var redisClient = redis.createClient(6379, 'localhost');
server = http.createServer(app);
server.listen(8080, function() {
console.log('listening on *:8080');
});
var io = require('./routes/index').listen(server);
var routes = require('./routes/index').router;
app.use(session({
secret: 'my-secret-string',
key: 'express.sid',
cookie: { maxAge: 2628000000 },
store: new (require('express-sessions'))({
storage: 'redis',
instance: redisClient,
host: 'localhost',
port: 6379,
}),
resave: true,
saveUninitialized: true
}));
app.use('/', routes);
module.exports = app;
and in my index.js:
var express = require('express');
var router = express.Router();
var socketio = require('socket.io');
var io;
router.get('/', function(req, res) {
//do stuff here...
res.render('index', { params: params });
}
var listen = function(app) {
io = socketio.listen(app);
io.sockets.on('connection', function (socket) {
console.log(socket.handshake); // there is an handshake object with cookie information
// which i can parse the express.sid (=sessionID) from
socket.on('someEvent', function(args, callback) {
//do stuff here
callback(err, msg);
});
});
return io;
}
module.exports = {
listen: listen,
router: router
}
Now, when the client emits "someEvent", i want to read the session ID from the session cookie and do stuff with the session data.
I followed this guide, and this, but they seem to be outdated or do not fit quite my setup. Had have no luck so far. Help is greatly appreciated! This is my first node.js project..