node.js - socket.io session does not update when req.session changes -


i working on basic app user needs authenticate (with passportjs) , send message server socket.io. here nodejs code:

var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieparser = require('cookie-parser'); var bodyparser = require('body-parser'); var expresssession = require('express-session'); var mongoose = require('mongoose'); var passport = require('passport'); var localstrategy = require('passport-local').strategy;  var routes = require('./routes/index'); var api = require('./api/index'); var auth = require('./routes/auth');  var session = expresssession({secret: 'mysecret', resave: true, saveuninitialized: true}); var port = normalizeport(process.env.port || '3000');  var app = express(); var server = require('http').createserver(app); var io = require('socket.io')(server); var sharedsession = require('express-socket.io-session');  // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.set('port', port);  //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(logger('dev')); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: false })); app.use(cookieparser()); app.use(session); app.use(passport.initialize()); app.use(passport.session()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/bower_components', express.static(path.join(__dirname, 'bower_components')));  app.use('/', routes); app.use('/api', api); app.use('/auth', auth);  // passport config var user = require('./models/user'); passport.use(new localstrategy({     usernamefield: 'email' }, user.authenticate())); passport.use(require('./strategies/facebook')); passport.use(require('./strategies/google')); passport.serializeuser(function(user, done) {     done(null, { id: user._id }); }); passport.deserializeuser(function(obj, done) {     done(null, obj); });  io.use(sharedsession(session, {     autosave:true }));  io.on('connection', function(socket) {     console.log('user connected');     console.log(socket.handshake.session);     socket.on('message-new', function(data) {         console.log('receiving message create');         console.log(socket.handshake.session);     });      socket.on('disconnect', function() {         console.log('user disconnected');     }); });  server.listen(port); 

so can see use express-socket.io-session able access session in socket , supposed same req.session. @ first when user connected socket.io (but not authenticated), socket.handshake.session equal req.session so:

session { cookie:  { path: '/',  _expires: null,  originalmaxage: null,  httponly: true } } 

once authenticates, req.session becomes:

cookie:   { path: '/',  _expires: null,  originalmaxage: null,  httponly: true }, passport: { user: { id: 56a13e58150f42fc29b44b7a } } } 

but when emit message-new client , displays socket.handshake.session, still equals

session { cookie:  { path: '/',  _expires: null,  originalmaxage: null,  httponly: true } } 

so socket's session not updated when req.session was. how can change behavior? need able know in socket if user connected or not...

inside event listener 'new message' can tell session reload, docs don't explicitly this, shares of same methods express-session

inside event listener can tell session reload

socket.handshake.session.reload(function(err) {     // give update session info } 

with 'express-session' can use same method in docs


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -