Hasan Teymoori
Hasan Teymoori

Reputation: 192

laravel broadcasting in admin guard

In my laravel website , we have two guard admin and customer this guard's tables is ->customer table and admin table.

I would like to send the notification from a customer to an admin via the laravel broadcasting this is my codes in the .js file into the admin guard

server.js files:

window.Echo = new Echo({

   broadcaster: 'socket.io',
   host: window.location.hostname + ':6001',
}); 

* i have a admin by id=2 *

Echo.private('App.Models.Admin.2')
    .notification((notification) => {
        console.log(notification.type);
    });

and into the customer guard:

\Notification::send(Admin::find(2) , new SimpleAlert('hey bro'));

SimpleAlert.php:

public function toBroadcast($notifiable)
{
    return new BroadcastMessage([
        'message' => $this->message,
        'type' => 'broadcast'
    ]);
}

so, i start the laravel-echo-server and open two google chrome tab, one for admin guard and another for customer guardand logged into the both guard.
No error into console. No error into customer sending notification, but `admin not receive the notification.

Problem:
laravel-echo-server showing this when i into the admin guard

Client can not be authenticated, got HTTP status 403

help me please.

Upvotes: 1

Views: 1171

Answers (2)

Big Pete
Big Pete

Reputation: 146

This answer did not work for me. The reason it doesn't work is because you are comparing the intended id ($userId) to an authenticated user on the user table and not the admin table. To send a message on a private channel to an admin person this worked for me:

in routes/channels.php.

Broadcast::channel('App.Models.Admin.{userId}', function ($user, $userId) {
    return auth()->guard('admin')->user()->id === $userId;
});

Upvotes: 1

rkj
rkj

Reputation: 8297

What you can do is add the authorization in routes/channels.php. Try this

Broadcast::channel('App.Models.Admin.{userId}', function ($user, $userId) {
    return $user->id === $userId;
});

Upvotes: 1

Related Questions