How do I share sessions with express and websockets using connect-sqlite3?
Esowteric opened this issue · comments
I need to share sessions between express-session (v4) GET requests and WebSockets on('connection') using connect-sqlite3.
The following basic setup without sqlite3 (from Azmisov's answer at https://stackoverflow.com/questions/12182651/expressjs-websocket-session-sharing) works:
global.app = express();
var sessionParser = require('express-session')({
secret:"some secret",
resave: true,
saveUninitialized: true
});
app.use(sessionParser);
app.get('*', function (req, res) {
req.session.working = "Yes";
});
wss.on('connection', function connection(ws, req) {
sessionParser(req, {}, function(){
console.log("New websocket connection: Working = " + req.session.working + ".");
});
});
However, when I try to implement this with connect-sqlite3:
var sessionParser = require('express-session');
var SQLiteStore = require('connect-sqlite3')(sessionParser);
app.use(sessionParser({
store: new SQLiteStore({dir:configuration.data_dir, db:'sessions.s3db', table:'sessions',}),
secret: 'some secret',
resave: false,
saveUninitialized: true,
cookie: {
maxAge: configuration.session_cookie_maxage_ms,
name: 'shadowlands'
}
}));
app.get('*', function (req, res) {
req.session.working = "Yes";
});
wss.on('connection', function connection(ws, req) {
sessionParser(req, {}, function(){
console.log("New websocket connection: Working = " + req.session.working + ".");
});
});
In wss on('connection') at sessionParser(req, {}, function(){ ...), I get:
express-session deprecated undefined resave option; provide resave option; provide resave option.
express-session deprecated undefined resave option; provide saveUninitialized option; provide saveUninitialized option.
express-session deprecated req.secret; provide secret option.
This error still occurs if I change to:
sessionParser(req, {secret: 'some secret', resave: false, saveUninitialized: true}, function(){
The error goes away if I take out "req":
sessionParser({secret: 'some secret', resave: false, saveUninitialized: true}, function(){
But then the sessionParser function does not get called, and req.session.working is not shown in the console.
Any help or an example in the readme much appreciated, thanks!
Eric T.
I managed to fix it using cookie-parser and cookie in the websocket on('connection').
Glad you were able to resolve it!