Search code examples
phplaravelsocket.iobroadcast

laravel broadcasting for multiple guard


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.


Solution

  • 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';
    });