Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
Synchronizing Express Session with Socket.IO

I'm facing an issue where I need to communicate with my Node.js server, as well as with my Socket.IO instance. That part works but I also need to access the sessions I have created with Express, even inside the Socket.IO listeners. To achieve that, I'm using express-socket.io-session. It technically works but not the way I want it to. When I make changes to the session, I use req.session.save(function(err) {}) to make sure it really is saved. I use socket.handshake.session.save(function(err) {}) inside the Socket.IO listeners as well. The sessions are saved but I noticed that while the session can be accessed as intended outside the listeners, using them inside any of my Socket.IO listeners isn't that... easy. They are unsynchronized. For example, if I run req.session.user = "abc", it will appear as undefined in a Socket.IO listener... unless I refresh the page. Thanks to that discovery and a few answers here, I realised that disconnecting and connecting Socket.IO from the client-side fixes this. But I'm not sure if that would be the best solution to the problem. Despite the fact that it works, there are scenarios where I can't just keep disconnecting and connecting again and again. After another... session of looking for solutions, I tried using socket.handshake.session.reload(function(err) {}) before doing anything else. Oddly enough, I think it sometimes does work. But even if that's true, the session remains with that undefined value most of the time. I use the following configuration:

var session = require("express-session")({
    // ...
    resave: false,
    saveUninitialized: false
});

I tried to use saveUninitialized: true instead and it seemed like it did synchronise the sessions but it did not respond to scenarios where the user had deleted their cookies manually, through the browser. In such a case, the session should really appear as undefined. And it does... just not inside the listeners. The documentations also say that using true has been deprecated.

I'm stuck with this issue and despite giving my best to find a solution, I couldn't. Are there other methods I can try?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!