2

First of all, this is how I am sharing the sessions. It basically follows the, implementation in this thread.

var sessionMiddleware = session({
  name: 'websiteName.sess',
  store: new RedisStore({
    client: redisClient,
    ttl: 60*60*24*7,
    db: 2 
  }),
  secret: 'secredCode',
  saveUninitialized: false,
  resave: true
});


io.use(function(socket, next) {
  sessionMiddleware(socket.request, socket.request.res, next);
});

// Express Session
app.use(sessionMiddleware);

This solution works fine on localhost. However, when I run the express app on a remote AWS server the session data is not shared. As a note, I am using elasticache redis, as the redis client.

After saving a user id to the express session this is what the session objects look like.

req.session:

{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"flash":{},"userId":"8ad5dce4b600xxxxxxxxx"}

socket.request.session:

{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}}

I am quite certain the problem is not that the session data not being saved, as the user id, is saved in the session the next time the user accesses the server.

Why is this happening? Is their a fix?

edit: This is my client side connection to socket.io:

  var socket = io.connect('http://ip.ad.dr.ess:80');
joshL
  • 67
  • 4

1 Answers1

2

Making my comment into an answer since it appears to have solved your issue:

You need to make sure the address in the URL bar of the browser is EXACTLY the same host/port like http://ip.ad.dr.ess:80? If not, then socket.io and the web page won't share cookies and thus you would miss the session cookie.

jfriend00
  • 580,699
  • 78
  • 809
  • 825