Search code examples
node.jssessionsocket.ioexpress-session

How to access session object in socket.io connection


I'm using express-session, and I set the session object on a login page. That all works fine. My socket.io connections aren't initiated and used until further into the site. Reading the socket.io documentation it mentions that you can access the request object that initiated the socket connection. This doesn't seem to be same as the express-session object viz:

io.on("connection", function (socket) {
    console.log(socket.request.session)
});

outputs undefined. There is a request object viz: console.log(socket.request) outputs [object Object].

Is there a way to access the express-session session object (that was set on a previous page before the socket connection was made) through a socket.io connection?


Solution

  • You can try using express-socket.io-session , the reference has information of how to share session from express-session.