1

I spent 2 days on accessing sessions (log user in) trough sockets.

I already have users in my mongodb, and i can indeed login/register them without sockets (trough post or get). All i need now is the same for sockets.

I have tried these SO solutions:

socket.io and express 4 sessions

How to share sessions with Socket.IO 1.x and Express 4.x?

socket.io and session?

And spend a lot of time googling. And trying different things.

I typically get undefined errors, or deprecated error, or nothing happens at all without errors.

After following the tutorials above, and doing some tweaks myself, my code went from "hard to read" to "too hard to read, must start from scratch". So i wont put up my code here.


Instead, can someone share their bare-minimum code on what it takes to access the sessions inside sockets? With explanations would would be highly appreciated.

UPDATE I followed this: How to share sessions with Socket.IO 1.x and Express 4.x?

And i got a few issues. It still does not work. the session is empty. Am i doing something wrong? Full code:

var express = require("express");
var Server = require("http").Server;
var session = require("express-session");
var RedisStore = require("connect-redis")(session);
var SESSION;
var app = express();
var server = Server(app);
var sio = require("socket.io")(server);

var sessionMiddleware = session({
    store: new RedisStore(),
    secret: "keyboard cat",
    resave: true,
    saveUninitialized: true
});

sio.use(function(socket, next) {
    sessionMiddleware(socket.request, socket.request.res, next);
});

app.use(sessionMiddleware);

app.get("/*", function(req, res){
    SESSION = req.session 
    req.session.name="THE NAME"; // <<<< SHOULDN'T THIS WORK? ITS UNDEFINED
    if(req.path=="/"){
        res.sendFile(__dirname+"/index.html");
    }else{
        res.sendFile(__dirname+req.path);
    };
});

sio.sockets.on("connection", function(socket) {
    SESSION = socket.request.session
    console.log("The SESSION variable on socket connection:");
    console.log(SESSION); //<<<<<<<<<<<< UNDEFINED
    socket.on("get session status",function(){  
        socket.emit("session status",{ SESSION:SESSION }); // <<<<< EMPTY OBJECT
    })
});

server.listen(80);
Community
  • 1
  • 1
Karandawov
  • 83
  • 1
  • 8

1 Answers1

1

The accepted answer at the second link works fine for me:

app.js:

var session = require("express-session");
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

var sessionMiddleware = session({
  secret: "keyboard cat",
  resave: true,
  saveUninitialized: true
});

app.use(sessionMiddleware);

app.use(function(req, res) {
  req.session.name = "THE NAME";
  res.sendFile(__dirname + "/index.html");
});

io.use(function(socket, next) {
  sessionMiddleware(socket.request, socket.request.res, next);
});

io.on("connection", function(socket) {
  console.log('socket.io connection');
  console.dir(socket.request.session);
  // above outputs:
  // socket.io connection
  // { cookie:
  //    { path: '/',
  //      _expires: null,
  //      originalMaxAge: null,
  //      httpOnly: true },
  //   name: 'THE NAME' }
});

server.listen(8000);

index.html:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:8000');
</script>

Perhaps there is an issue with timing where the session data isn't saved to Redis yet when the socket.io connection is made? You might try delaying the socket.io connection at least until the page is fully rendered (window.onload).

Community
  • 1
  • 1
mscdex
  • 93,083
  • 13
  • 170
  • 135