While utilizing socket.io with express and incorporating express session along with express-socket.io-session, I am encountering difficulty in accessing the properties of the express session within the socket.io session object, and vice versa.
const server = require("http").createServer(app);
const client = require("socket.io").listen(server);
session = require("express-session")({
secret: "my-secret",
resave: true,
saveUninitialized: true
}),
sharedsession = require("express-socket.io-session");
app.use(session);
client.use(sharedsession(session, {
autoSave:true
}));
client.on("connection", (socket) => {
socket.on("input", data => {
console.log(socket.handshake.session.user)
socket.handshake.session.name = "bar"
socket.handshake.session.save()
})
})
app.post("/signup", (req, res, next) => {
req.session.user = "foo";
})
app.get("/test", (req, res, next) => {
console.log(req.session.name)
})
When using both console.log()
, undefined is returned, indicating that they are indeed separate objects.