Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davorminchorov's avatar

How do you transfer the Laravel session over to NodeJS and use it together with Socket.io?

Hello!

Currently, I am working on a project which requires some real-time communication and I've decided to work with redis and socket.io instead of pusher.

When a user logs in to the web app, he/she gets a randomly generated socket ID which is great but it's anonymous by default.

  1. What I want to do is, assign each logged in user to it's socket ID so I know which user I send private notifications / messages to. Example: I want to send a message to the user John Doe only, but Jane Doe and John Smith shouldn't get that message / notification.
  2. Also, I'll need to send notifications / messages to a group of logged in users by role (admins for example)

According to some research, I need to share the Laravel session with NodeJS, which looks simple if I use redis but I've noticed that there's some encryption / decryption going on and something is missing in the examples.

Here are some of the links I've found:

Does anyone here know how this stuff works and maybe explain it to me in short? Some examples would be cool too.

I am using Laravel 5.1, Redis 3.0, NodeJS 5.3.0, ExpressJS 4.13.* and Socket.io v. 1.3.7 / 1.4.1

Thanks for reading!

0 likes
6 replies
davorminchorov's avatar

Yeah, I saw that link but I'll check it out! My app is traditional, not using any SPA frameworks on the front end.

I forgot to mention that the app will be for registered users only (invitation based).

davorminchorov's avatar

I also have another question: How do you implement Socket.io authentication?

davorminchorov's avatar

I've found this example: twostairs\node-socket-laravel-session but for some reason I encounter some strange bugs when I am trying to get the laravel_session from the cookie header. The problem is, I get the session for a moment and then it disappears.

It would be great if someone creates an npm package to handle the Laravel session on the node.js server and use it with Socket.io to identify logged in users!

Please or to participate in this conversation.