2

I'm new to socket.io. I've implemented a user login using express-session. In req.session.user I've stored the userinformatiuon so that I can access it later (it works). Now I want to acces these information in my socket. I've discovered that you can use the package express-socket.io-session to access the session in socket.io with socket.handshake.session. But the property socket.handshake.session.user is undefined when I try to access it.

It would be great if anyone knows how this works and could help me with this problem because I couldn't find any solution or help through searching the internet.

This: How to share sessions with Socket.IO 1.x and Express 4.x? for example didn't work for me.

Cheers

This is my code:

const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');

const expressSession = require('express-session');
const sharedsession = require("express-socket.io-session");

var app = express();

var http = require('http').createServer(app);
var io = require('socket.io').listen(http);

app.set('port', process.env.PORT || 3001);

app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(expressSession({
    key: 'userID',
    secret: 'k39dzqlp@k5d&k@dh&',
    saveUninitialized: false, 
    resave: false,
    cookie: {maxAge: 1200000}
}));

io.use(sharedsession(expressSession({
    key: 'userID',
    secret: 'k39dzqlp@k5d&k@dh&',
    saveUninitialized: false,
    resave: false, 
    cookie: {maxAge: 1200000}
})));


app.route('/login')
    .post((req, res) => {
         //I get user from my database
         req.session.user = user.dataValues;
         res.redirect('home');
        });
    });

app.listen(app.get('port'));

io.on('connection', (socket) => {
    console.log(socket.handshake.session.user); //user is undefined
});
Heide Nei
  • 21
  • 4
  • When is the socket connection made ? Before or after the post to /login ? – Malice Jan 19 '18 at 16:15
  • Do you mean on the server or client side? On the server side before the post. On the client side the script is included at the end of the file – Heide Nei Jan 19 '18 at 16:52
  • 1
    Could it anyway happen that the socket connection is happening before the session is set ? – Malice Jan 19 '18 at 18:44
  • `socket.handshake.session` is set when the socket.io connection is first established so for that to work, the user must be logged in BEFORE the socket.io connection is established. If the login is done via a form submission (not via Ajax), then this is not an issue because every login causes a new page to be loaded. But, if the login is only via Ajax, then you may have to re-establish the socket.io connection after a successful login. – jfriend00 Jan 20 '18 at 05:55
  • The Login is done via a Form Submission from a different file than my socket Connection. So req.socket.user happens before the client establishes a socket Connection. If I have understood correctly this should be ok because the Session ist already set before socket.handshake.session is set. Or am I wrong? – Heide Nei Jan 20 '18 at 09:51
  • Have same problem.. you didn't solve?! Nowhere could a solution be found. – Suisse Aug 07 '21 at 13:17

0 Answers0