Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda