I have this nodejs server that's using socket.io and express session so it can share the sessions to socket.io.
Whenever an user is losing the connection it automatically reconnects and as soon as it reconnects it tries to emit data to the server. All good at this point, everything works properly, but when the server is restarted (ONLY IN THIS CASE), the emitted event sometimes doesn't fire and I am 100% sure it is executed on the client side.
I believe it has something to do with the session sharing because as soon as I remove it, all the issues are gone.
I did some digging around, enabled debug, but still couldn't find anything or at least anyone else having the same problem.
Client:
socket.on('connect', function() {
if(!disconnected) // make sure the socket has been disconnected first and is not a fresh connection
return;
notify_client("Connection restored");
socket.emit('/v2/findSession.jx', { auth: localStorage.getItem('Session-Auth') }, function(data) {
console.log(">> Event fired!");
})
connectionError = false;
disconnected = false;
})
Server:
var express = require('express');
var app = express();
var expressSession = require('express-session');
var mysqlSession = require('express-mysql-session')(expressSession);
var mysql = require('mysql');
var sharingSession = expressSession({
secret: 'esLDkDgHSqvn',
resave: true,
saveUninitialized: true,
cookie: {
maxAge: 60*60*48*1000
},
store: new mysqlSession({
user: 'root',
password: 'root',
database: 'rootdb',
checkExpirationInterval: 900000,
expiration: 60*60*48*1000
})
})
app.use(sharingSession);
app.set('port', process.env.PORT || 80);
var server = require("http").createServer(app);
var ioSession = require("express-socket.io-session");
var io = require("socket.io")(server);
io.use(ioSession(sharingSession, {
autoSave: true
}))
io.on('connection', function(client) {
console.log(">> Client connected!");
client.on('/v2/findSession.jx', function(data, fn) {
console.log(">> Event fired!");
return fn({ result: true });
}
}
server.listen(app.get("port"));
I also tried sharing the session without the express-socket.io-session middleware and got the same result
io.use(function(socket, next) {
sharingSession(socket.handshake, {}, next);
})
Debug without session sharing (everything works fine in this one): https://pastebin.com/eyrvxHWv Debug with session sharing: https://pastebin.com/4k2gjky4
I apologise for the pastebin links, but I had to use them since the logs are long enough. Any help would be handy cause I'm starting to lose all my hopes with it.
via chosen
No comments:
Post a Comment