Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Echo - Allow guests to connect to presence channel

I am using laravel-echo-server to run Laravel Echo to broadcast events.

I have a user counter channel which shows all the users on the app. For this I am using a presence channel. This works fine for logged in users, but guests just never get connected.

I've setup the below in the BroadcastServiceProvider:

Broadcast::channel('global', function () { return ['name' => 'guest']; });

Which from what I can tell, should allow everyone in as 'guests'. I'm guessing there's some middleware or auth that's being checked before this that I need to disable for this channel.

Any help on getting all clients joining this presence channel would be much appreciated!

like image 834
Josh Undefined Avatar asked Apr 11 '17 09:04

Josh Undefined


3 Answers

For anyone looking for answers to this. It is indeed possible to auth guests into presence channels you just need to override the Broadcast::routes() from the service provider with your own.

As an example my presence channel 'global' accepts guests:

Route::post('/broadcasting/auth', function(Illuminate\Http\Request $req) { if($req->channel_name == 'presence-global'){return 'global';} return abort(403); });

This could be extended in various directions, or could continue to pass other presence and private channels through to the default Broadcast::auth method

like image 118
Josh Undefined Avatar answered Nov 18 '22 11:11

Josh Undefined


You may create a temporary user with factory(User::class)->make(...) and authenticate it with a middleware to use it as a guest.

Step 1: Creating the middleware

Run: php artisan make:middleware AuthenticateGuest

In app/Http/Middleware/AuthenticateGuest.php:

public function handle($request, Closure $next)
{
    Auth::login(factory(User::class)->make([
        'id' => (int) str_replace('.', '', microtime(true))
    ]));

    return $next($request);
}

Now setup the AuthenticateGuest middleware in Kernel.php.

In app\Http\Kernel.php:

protected $routeMiddleware = [
    ...
    'authenticate-guest' => \App\Http\Middleware\AuthenticateGuest::class,
];

Step 2: Setup Broadcast::channel route

In routes/channels.php:

Broadcast::channel('chatroom', function ($user) {
    return $user; // here will return the guest user object
});

More at: https://laravel.com/docs/8.x/broadcasting#authorizing-presence-channels

like image 40
Renan Coelho Avatar answered Nov 18 '22 12:11

Renan Coelho


The other solutions didn't work for me for a guest presence channel, this is what I ended up with:

// routes/channels.php

<?php
use Illuminate\Auth\GenericUser;

/*
|--------------------------------------------------------------------------
| Broadcast Channels
|--------------------------------------------------------------------------
|
| Here you may register all of the event broadcasting channels that your
| application supports. The given channel authorization callbacks are
| used to check if an authenticated user can listen to the channel.
|
*/

Route::post('/custom/broadcast/auth/route', function () {
    $user = new GenericUser(['id' => microtime()]);

    request()->setUserResolver(function () use ($user) {
        return $user;
    });

    return Broadcast::auth(request());
});

Broadcast::channel('online.{uuid}', function ($user, $uuid) {
    return [
        'id' => $user->id,
        'uuid' => $uuid
    ];
});



like image 4
jbwilhite Avatar answered Nov 18 '22 12:11

jbwilhite