express-socket.io-session: Undefined

Trying to let the code speak for itself.

var express = require("express");
var app = express();
var server = require("http").createServer(app);
var io = require("socket.io")(server);
var session = require('express-session')({
    name: "session",
    secret: "lfdod9Jiru65MMl",
    saveUninitialized: true,
    resave: true,
    cookie: {
        httpOnly: false,
        maxAge: 1000 * 60 * 60 * 24
    }
});
var sharedsession = require("express-socket.io-session");

app.use(session);
io.use(sharedsession(session), {
    autoSave:true
});
io.on('connection', function(socket){
    socket.on("clicked", function(value) {
       // this does happen, I checked with console.logs
       socket.handshake.session.key = value;
    });
});
`app.get("/index", function(req, res) {
    console.log("xxx");
    console.log(req.session.key); //writes undefined
    res.render("index");
});

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 19

Most upvoted comments

Did you ever get to the bottom of this? I’m having the same issue. I’m solely using the default namespace.

Seems to work without passport.socketio. To be honest, after having worked through this I don’t think I need passport.socketio at all. With express-socket.io-session I guess I can see if the user is authenticated by checking for socket.handshake.session.passport.user and even check that their JWT expiry at any time.