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

Losing socket events #21

Closed
slpn1 opened this Issue Nov 28, 2016 · 2 comments

Comments

Projects
None yet
4 participants
@slpn1
Copy link

slpn1 commented Nov 28, 2016

Hi, when I use this I lose all socket events with no errors. As soon as I comment out the sharedsession middleware it all works as expected. Any ideas?

var express = require('express')
  , app = express()
  , bodyParser = require('body-parser')
  , port = process.env.PORT || 3000
  , router = express.Router()

var cookieParser = require('cookie-parser')
app.use(cookieParser('AM6YjAA'));

var session = require('express-session');
var redisStore = require('connect-redis')(session);

if (process.env.REDIS_URL) {
  var rtg = require('url').parse(process.env.REDIS_URL);
  redis = require('redis').createClient(rtg.port, rtg.hostname);
  redis.auth(rtg.auth.split(':')[1]);
} else {
  redis = require('redis').createClient();
}

app.use(session(
  { secret: 'AM6YjAA', 
    resave: true,
    saveUninitialized: false,
    store: new redisStore({ client: redis,ttl :  260})
   }
));
 
var sharedsession = require("express-socket.io-session");
var ioServer = io = require('socket.io');
io = new ioServer();
//io.use(sharedsession(session));


app.set('views', __dirname + '/views')

// set the view engine to ejs
app.set('view engine', 'ejs');

app.use(express.static(__dirname + '/public'))
app.use(express.static(__dirname + '/node_modules'))
app.use(bodyParser.json())
app.use(require('./controllers'))

app.listen(port, function () {
  console.log('Listening on port ' + port);
  io.attach(this);
})

io.on('connection', (socket) => {
      console.log('user connected');
      socket.on('disconnect', () => {
        console.log('user disconnected');
      });
});
@mmckenziedev

This comment has been minimized.

Copy link

mmckenziedev commented Mar 28, 2017

@slpn1 If you follow the example at https://github.com/oskosk/express-socket.io-session#usage you will see that your code should be written like this:

var sessionMiddleware = session(
  { secret: 'AM6YjAA', 
    resave: true,
    saveUninitialized: false,
    store: new redisStore({ client: redis,ttl :  260})
   }
);

app.use(sessionMiddleware);
 
var sharedsession = require("express-socket.io-session");
var ioServer = io = require('socket.io');
io = new ioServer();
io.use(sharedsession(sessionMiddleware));
@adam4813

This comment has been minimized.

Copy link

adam4813 commented Nov 22, 2017

I was doing the same thing with not storing session and using it in that manner. Instead I was creating a new one in both express via app.use(session({})) and sharedSession(session).

@oskosk oskosk closed this Jan 25, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment