question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sharing session with Socket.io both ways

See original GitHub issue

I’ve seen a number of tutorials of how to share an express session with socket.io. However, they all usually show how to SET from express and then GET from within the socket connection. (server)

Is there a way to do this where you can SET from within the socket connection?

io.on('connection', function(socket){
  socket.request.session.userdata = mydata;
});

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
dougwilsoncommented, Sep 11, 2015
var express = require('express')
var expressSession = require('express-session')
var http = require('http')
var io = require('socket.io')

var session = expressSession({ /* configuration */ })

// middleware
app.use(session)

// routes
app.get('/', function() {})

// setup servers
var server = http.createServer(app);
var sio = io.listen(server);

// setup socket auth with sessions
sio.set('authorization', function(handshake, accept) {
  session(handshake, {}, function (err) {
    if (err) return accept(err)
    var session = socket.handshake.session;
    // check the session is valid
    accept(null, session.userid != null)
  })
})

// setup socket connections to have the session on them
sio.sockets.on('connection', function (socket) {
  session(socket.handshake, {}, function (err) {
    if (err) { /* handle error */ }
    var session = socket.handshake.session;
    // do stuff

    // alter session
    session.userdata = mydata

    // and save session
    session.save(function (err) { /* handle error */ })
  })
})
0reactions
knoxcardcommented, Jun 1, 2018

Funny, I actually had to read this post to do the opposite. My NodeJs controller /logout was being called but my socket.io middleware kept bringing the user session back to life. Had to get rid of session.touch.save() and now everything works great!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use with `express-session` | Socket.IO
There are two ways to share the session context between Express and Socket.IO, depending on your use case:
Read more >
How to share sessions with Socket.IO 1.x and Express 4.x?
The solution is surprisingly simple. It's just not very well documented. It is possible to use the express session middleware as a Socket.IO...
Read more >
The Importance of Documentation or “How i discovered how to ...
I found myself in this situation quite recently while building a project, i wanted a way to share an Express session with Socket.io...
Read more >
Sharing sessions between SocketIO and Express using Redis
In this post I'll explain how to share session data between SocketIO and NodeJS Express framework by storing session data to Redis.
Read more >
Socket.io - shared session with Express : r/node - Reddit
socket.on("set-data", function(data) { socket.handshake.session.data = data; socket.handshake.session.save ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found