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

davorminchorov's avatar

Sharing the Laravel session with NodeJS for Socket.io Video/Series Request

Hey @JeffreyWay,

I know that this might sound way too advanced for some people but I'd love to see a video (or even a series) on sharing the laravel session with nodejs so socket.io knows which socket ID belongs to which User ID (or has an idea which user is currently online)

By default, socket IDs are anonymous and they refresh on page load. When you add authentication to an app, here comes the interesting part. How would you know if Jimmy is logged in and has a socket ID?

Sharing sessions between Laravel and Node is so rare on the internet (I couldn't find too much info on it) but it's very useful!

Thanks!

0 likes
13 replies
cklmercer's avatar

+1 for sure. I would love to see this video. I struggled with this for many hours.

spearmootz's avatar

I had found a good solution for this about a year ago. I decided to make it a module, its really easy to use. helps you get the cookie without hard coding it. helps you get that session Id and retrieve it from mysql and redis

https://www.npmjs.com/package/node-laravel-session

this way you can share all of your session. you can also utilize the session in order to sign up a user to certain rooms

1 like
davorminchorov's avatar

@spearmootz I've found a different approach and I've finished the feature few months ago. Thanks for sharing the package!

sarah's avatar

please Ruffles could you share us your solution and is it secure ?

davorminchorov's avatar

I executed and artisan command on the node server to get the user data and attach it to the socket object so I have it anywhere on my nodeJS server.

kocoten1992's avatar

@spearmootz , I try your package for a while and got no success

let session = cookie.parse(req.headers.cookie).laravel_session;

I understand req is request, but where it coming from, even try install express but got no success, could you clarify it more on tutorial?

P/s: haven't get to that, but redisConnection, how it should be format, where it comming from too, thank you :)

kocoten1992's avatar

@Ruffles , how you find back the user from node? I'm trying your method:

var server = require('http').createServer()
var cookie = require('cookie')
var io = require('socket.io')(server)

io.on('connection', function(socketObject) {
    var cookies = cookie.parse(socketObject.handshake.headers.cookie)
    console.log(cookies.laravel_session)
})

server.listen(3000)

I can take out the laravel_session, but how from that can trace back to user object (can't find it from laravel api), or you using different approach, please guide me :D

davorminchorov's avatar

I executed and artisan command on the node server to get the user data and attach it to the socket object so I have it anywhere on my nodeJS server.

I didn't use any cookies or session for this. I tried it but it seemed a little bit more complicated, I needed to get the session and do some encode / decode magic but I failed.

kocoten1992's avatar

I'm still experimenting, will update you when it done @Ruffles , currently it looking nice enough:

php artisan tinker
use Illuminate\Auth\AuthManager;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Bootstrap\LoadConfiguration;
use Illuminate\Foundation\Bootstrap\SetRequestForConsole;


$app = new Application(getcwd());
$auth = new AuthManager($app);

$l = new LoadConfiguration;
$l->bootstrap($app);

$s = new SetRequestForConsole;
$s->bootstrap($app);

$app->registerConfiguredProviders();

$auth->check(); //false, currently I'm passing no session to it

(It look simple, but I put up a real good fight with it)

kocoten1992's avatar

@Ruffles , damn hard, but I crack it

php artisan tinker
use Illuminate\Http\Request;
use Illuminate\Encryption\Encrypter;
use Illuminate\Auth\AuthServiceProvider;
use Illuminate\Foundation\Bootstrap\LoadConfiguration;
use Illuminate\Foundation\Bootstrap\SetRequestForConsole;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;


$key = base64_decode('HoEXdquDDDXR20pZy7QleR7FTGZZXix1JeHwDv5Hhh8=');

$sr = new SymfonyRequest;
$sr->initialize([], [], [], ['laravel_session' => 'eyJpdiI6IjhOcFROekxpamU1U0oyYjIzZkw2TlE9PSIsInZhbHVlIjoiZUo0TmJTZURuS2RTVXc1SVY0cWU3XC9BcVwva2NnUFAzUmI0K2NwTXlzd3BibEpncFwvdzZKNEZpMTlmaWxLZHRmTGd5cXZPTWszS1BuNG5vOGRVWkVKSFE9PSIsIm1hYyI6Ijg3MzFhMTAzZDUwZjdmZjY4MDZkYmY5ZDQ3ZDA1ZTYxOWEwYmNiYTBhNmI0ZTNhY2Q2NzMzY2YzMTIzMjdhYTQifQ==']);

$app = new Illuminate\Foundation\Application(getcwd());

$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Kernel::class
);

$app->handle($sr);

$auth = new Illuminate\Auth\AuthManager($app);

$auth->user(); //miracle :D
davorminchorov's avatar

cool, I would've never thought of doing it this way :)

1 like

Please or to participate in this conversation.