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!
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
@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
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.
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)
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