Sunday, October 22, 2017

Socket.io not sharing sessions with express

Leave a Comment

I can't figure why this is happening exactly since it was working perfectly before.

I'm using the following libraries:

"express-socket.io-session": "^1.3.2" "socket.io": "^2.0.3" "express": "^4.15.4" "express-session": "^1.15.5" 

1 - I login the user via a http request and send the cookie back to the frontend. All operations on http work perfectly with the cookie in frontend backend exchanges.

2 - After the user is logged in I tell the frontend "ok, user is logged in, now connect to the sockets":

io ( this.url ); 

Here is the relevant code:

var io_session = require("express-socket.io-session"); var e_session = require("express-session");  var sessionFileStore = require('session-file-store')(e_session);  var ee_session = e_session({     store: new sessionFileStore({ path: './user_sessions' }),     secret: "something-random",     resave: true,     saveUninitialized: true });  var enableCORS = function(req, res, next) {     res.header('Access-Control-Allow-Origin', req.headers.origin);     res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');     res.header('Access-Control-Allow-Headers', 'Origin, Accept, Content-Type, Authorization, Content-Length, X-Requested-With, *');     res.header('Access-Control-Allow-Credentials',true);          // intercept OPTIONS method     if ('OPTIONS' == req.method) {         res.sendStatus(200);     } else {         next();     }; };  app.use(function(req, res, next) {         ///    next(); });  //app.use(cookieParser()); app.use(enableCORS); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use(ee_session);  preparedApp = require("http").Server(app);  var io = require("socket.io")(preparedApp);  io.use(io_session(e_session,{     autoSave: true }));  preparedApp.listen(8080, function(){});  io.on('connection', function(socket){      var socket_session = socket.handshake.session,         socket_session_id = socket.handshake.sessionID;      console.log("SOCKET_SESSION:",socket_session);     console.log("SOCKET_SESSION_ID:",socket_session_id);      (...) 

3 - socket_session is 'empty' but everything in the regular http cookies works. The session is maintained there.

One thing I noticed is that socket_session_id points to a session file that does not exist inside the folder user_sessions. The only ones that exist are created in the http login. So, basically:

=> User logins: efihaeif939311kf3f3 session id file is created.

=> Socket connects: fiaejgieofaekofek is the session id in the same login flow but file does not exist in user_sessions (note that the session id is not the same)

Any idea on why this is happening? I have absolutely no idea.

Thanks

1 Answers

Answers 1

try sharing the session by stringifying the session json and retrieving and parsing back to session object in the client side.

This might work.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment