express-socket.io-session: Express socket io session no longer sharing sessions with express
For some odd reason this library is no longer working with my nodejs configurations. The sessionID of the socket session is never the same generated after the HTTP login.
I login the user via http and start his session. Then, I connect to the sockets. However, the socket connection always generates a new socketID.
I tried to DEBUG=express-socket.io-session but still I could not find the problem.
Here is my code and info
"express-socket.io-session": "^1.3.2"
"socket.io": "^2.0.3"
"express": "^4.15.4"
"express-session": "^1.15.5"
var io_session = require("express-socket.io-session");
var e_session = require("express-session");
var sessionFileStore = require('session-file-store')(e_session);
var ee_session = e_session({
store: new sessionFileStore({ path: './user_sessions' }),
secret: "something-random",
resave: true,
saveUninitialized: true
});
var enableCORS = function(req, res, next) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, Accept, Content-Type, Authorization, Content-Length, X-Requested-With, *');
res.header('Access-Control-Allow-Credentials',true);
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.sendStatus(200);
} else {
next();
};
};
app.use(function(req, res, next) {
///
next();
});
//app.use(cookieParser());
app.use(enableCORS);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(ee_session);
preparedApp = require("http").Server(app);
var io = require("socket.io")(preparedApp);
io.use(io_session(e_session,{
autoSave: true
}));
preparedApp.listen(8080, function(){});
io.on('connection', function(socket){
var socket_session = socket.handshake.session,
socket_session_id = socket.handshake.sessionID;
console.log("SOCKET_SESSION:",socket_session); // NOTHING IN COMMON WITH THE HTTP SESSION
console.log("SOCKET_SESSION_ID:",socket_session_id); // Different session ID from the http one.
(...)
Other things I noticed:
1 - The http session is always the same and is maintained 2 - The socket sessions is always the same and is maintained 3 - They are both different. As if only the http sessions are being accessed in ./user_sessions and the socket ones are not.
Any idea?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 25 (6 by maintainers)
@oskosk I solved it.
Http was reaching 127.0.0.1:8080
Socket.io was reaching localhost:8080
Just FML and F cross-domain requests.
Thank you so much for your help anyway @oskosk. For real, thank you! I’ll keep on using your library 😃
This is, quite literally, draining the life force out of me by now
Okok @oskosk thanks, let me know 😃