laravel broadcasting for multiple guard

十年热恋 提交于 2021-02-06 09:32:06

问题


I have the below auth guards that is defined for my app admins, designers, customers and etc. the default guard is the designer guard.

I want every guard to have his own private channel. So I am defining it in my channel.php with multiple entries for each like below

Broadcast::channel('private.admins.{id}', function ($admin, $id) {


    Log::info($admin);
    //logging the admin

});

But this is always binding with default guard class so my question is how do I tell that to use here Admin model. I am unable to find it anywhere. So can you point me in to right direction

Actually I want every guard to have his own private channel.


回答1:


Try changing in BroadcastServiceProvider file app\Providers\BroadcastServiceProvider.php

Different broadcast auth end point for each guards

public function boot()
{
   //Broadcast::routes();
   //match any of the 3 auth guards
   Broadcast::routes(['middleware' => ['web','auth:admins,designers,customers']]);
   require base_path('routes/channels.php');
}

Now in channels.php

Broadcast::channel('admins.channel.{id}', function ($model, $id) {
      return $model->id === $id && get_class($model) === 'App\Admin';
});

Broadcast::channel('designers.channel.{id}', function ($model, $id) {
      return $model->id === $id && get_class($model) === 'App\Designer';
});

Broadcast::channel('customers.channel.{id}', function ($model, $id) {
      return $model->id === $id && get_class($model) === 'App\Customer';
});



回答2:


I am Posting this answer, For every one who might face the problem no-days. I am using laravel 7 and beyondcode/laravel-websockets. As I dig in the source code specifying midllewares in BoradcastServiceProvider.php will not work. The only way to define a guard for a channel is by specifying options for the channel:

 Broadcast::channel('messaging.organ.{id}', function ($organ , $id) {
    return $organ->id == $id && get_class($organ) === "App\Organization";
} , ['guards' => ['organ']]);

Reason: because I am using beyondcode/laravel-websockets so I dig in src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php and in this file the retrieveUser method will get the user. in this file if an option for the channel is provided the user in the specified guards will be returned. you can define one or multiple guards, however it will only returns one user who is logged in as a guard that comes first in the array.

    protected function retrieveUser($request, $channel)
{
    $options = $this->retrieveChannelOptions($channel);

    $guards = $options['guards'] ?? null;

    if (is_null($guards)) {
        return $request->user();
    }

    foreach (Arr::wrap($guards) as $guard) {
        if ($user = $request->user($guard)) {
            return $user;
        }
    }
}


来源:https://stackoverflow.com/questions/51007102/laravel-broadcasting-for-multiple-guard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!