Skip to content
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

Using pm2 with session management #286

Closed
pyryson opened this issue Feb 3, 2014 · 4 comments
Closed

Using pm2 with session management #286

pyryson opened this issue Feb 3, 2014 · 4 comments

Comments

@pyryson
Copy link

pyryson commented Feb 3, 2014

I use session based authentication using Passport.js. Web I run it as a single threaded process, everything works as expected. But when I run it using pm2 cluster-mode the troubles begin. Requests are being handled in different cores time to time so sessions die occasionally. Can this problem be prevented in any other way than for example using Redis to store sessions?

Any pm2 built in way to do this?

@rlidwka
Copy link
Collaborator

rlidwka commented Feb 3, 2014

There are a lot of ways to solve this.

  1. run in fork mode (or in cluster mode with just one process)
  2. store sessions somewhere (i.e. Redis)
  3. use self-contained tokens in sessions
  4. patch cluster.js so it will load-balance using ip addresses

I'd really recommend to store sessions in the database. It is how it supposed to work.

Self-contained tokens is also an interesting idea. You encrypt session information inside your session token, and sign it to prevent tampering. It can be decrypted on every node provided they have a shared secret.

@pyryson
Copy link
Author

pyryson commented Feb 4, 2014

I solved this using option 2., though using memcached instead of redis. Server I use had memcached installed out of the box. Thanks!

@pyryson pyryson closed this as completed Feb 4, 2014
@pyryson
Copy link
Author

pyryson commented Feb 4, 2014

Semi-offtopic and semi-non-offtopic. I'm quite new to clustering. So I was wondering that is there any ways to use this manner when using sockets.io? I mean in process that is used in clustered mode with pm2? By default handshakes between clustered processes and and clients would happen by random in clustered mode using sockets.io, am I right? There is a same analogy with socket.io handshakes and i.e. login http-sessions?

@matthew-dean
Copy link

Are there any examples using pm2 with Redis? Redis seems to work until I start pm2 for more than one process. That is, sessions don't seem to be shared between processes. Any help appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants