0

I have app in node.js using socket.io for websockets(live chat) it runs on live-chat.dev domain.

Now i have another project runs on localhost where i'm connecting to live-chat.dev (client side).

I identify users by session.id that stores in node.js app.

The problem is : every page reloads brings new session.id generated.

config of node.js session middlware :

var sessionMiddleWare  = session({
  secret: 'livechat key',
  resave: false,
  saveUninitialized: true,
  cookie: {
    domain : 'localhost'
  }
});

io.use(function(socket, next) {
    sessionMiddleWare(socket.request, socket.request.res, next);
});
Farhad
  • 742
  • 6
  • 22

1 Answers1

0

I think what you are looking for is persistent session. You can achieve this by storing your session object in any data store and fetching it form there.

If you are comfortable with mongodb then you can use connect-mongo library.

You can read more about it here https://github.com/jdesboeufs/connect-mongo

Akshay Kumar
  • 576
  • 2
  • 10
  • But the problem is that my apps running on different hostnames, and that is why socket io gives me new session id every time. If i run two project on one host. everything works fine – Farhad Dec 14 '16 at 10:26
  • Then I think in your case you will have to write a custom middleware and handle both the domains as one. Also you can read this http://stackoverflow.com/questions/19104292/express-session-with-different-cookie-domain-per-request it may help you. – Akshay Kumar Dec 14 '16 at 10:40
  • it didn't helped, i think the problem is in that localhost project doesn't set coockie when receives it. Thank you for trying to help! – Farhad Dec 14 '16 at 12:54