-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgraded to 4.x and objects in socket.request.session no longer exists #3933
Comments
Downgraded back to 2.4.1 (for the 5th time), changed the imports from named to default and |
I'm having the similar issue with the following versions: |
I also have this bug using: express 4.17.1 Please fix this bug, we need the session to be shared between express and socket.io |
OK, so I think I found the culprit. The middleware at the Socket.IO level only has access to the To be able to add the cookie, we need to intercept the handshake: const io = new Server(httpServer, {
allowRequest: (req, callback) => {
// with HTTP long-polling, we have access to the HTTP response here, but this is not
// the case with WebSocket, so we provide a dummy response object
const fakeRes = {
getHeader() {
return [];
},
setHeader(key, values) {
req.cookieHolder = values[0];
},
writeHead() {}
}
sessionMiddleware(req, fakeRes, () => {
if (req.session) {
// trigger the setHeader() above
fakeRes.writeHead();
// manually save the session (normally triggered by res.end())
req.session.save();
}
callback(null, true);
});
}
});
io.engine.on("initial_headers", (headers, req) => {
if (req.cookieHolder) {
headers["set-cookie"] = req.cookieHolder;
delete req.cookieHolder;
}
}); That sounds a bit far-fetched, but it should do the trick. I will add an example in the documentation. |
OK, so I've added an example in the documentation: https://socket.io/how-to/use-with-express-session Please ping me if something is missing! |
I've decided to try upgrading to 4.x today from 2.x and running into issues with properties in With that set up, I also have to use the session middleware depending on which namespace the client is connecting from. For example, if they are connecting from I've followed the guide you posted, but the issue I am running into is setting up the server options using the example posted above here. I try just using one of my session middleware with your example but I am still not getting
And then in
I log into my app, emit the
#4383 got it working. So what am I doing wrong? And with a 4 session middleware setup, how do I configure the server options for 4 middlewares? |
This commit implements middlewares at the Engine.IO level, because Socket.IO middlewares are meant for namespace authorization and are not executed during a classic HTTP request/response cycle. A workaround was possible by using the allowRequest option and the "headers" event, but this feels way cleaner and works with upgrade requests too. Syntax: ```js engine.use((req, res, next) => { // do something next(); }); // with express-session import session from "express-session"; engine.use(session({ secret: "keyboard cat", resave: false, saveUninitialized: true, cookie: { secure: true } }); // with helmet import helmet from "helmet"; engine.use(helmet()); ``` Related: - #668 - #651 - socketio/socket.io#4609 - socketio/socket.io#3933 - a lot of other issues asking for compatibility with express-session
This does not work... There is still no data in session object. |
Describe the bug
socket.request.session
is always a new session even after declaringreq.session.user
with a value.To Reproduce
Setup Socket.io with
express-session
middleware.Socket.IO server version:
^4.1.1
Server
Socket.IO client version:
^4.1.1
Client
Expected behavior
socket.request.session
should haveuser
property in it when initializing session in express middleware (eg. -req.session.user = user
)Platform:
Additional context
The text was updated successfully, but these errors were encountered: