Private Broadcast Route Admin
January 27, 2025 ยท AI Conversations
### 1. **Setup Laravel Echo and Broadcasting**
Ensure that Laravel broadcasting is properly configured. In `config/broadcasting.php`, set up your default broadcaster (e.g., Pusher, Redis, etc.).
### 2. **Define a Broadcast Route**
In your `routes/channels.php` file, define a private channel for the `admin` guard:
use IlluminateSupportFacadesBroadcast;
Broadcast::channel('admin.{id}', function ($user, $id) {
// Ensure the user is authenticated as an admin
return $user->id === (int) $id && $user->guard_name === 'admin';
});
### 3. **Modify the Guard Configuration**
Make sure your `admin` guard is correctly configured in `config/auth.php`. Example:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => AppModelsUser::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => AppModelsAdmin::class,
],
],
### 4. **Authenticate the Admin in `Broadcast::channel`**
In the `Broadcast::channel` callback, verify that the authenticated user belongs to the `admin` guard. Use the following approach:
– Use `Auth::guard('admin')` to ensure the `admin` guard is active.
– Validate the `$id` to ensure it matches the admin user.
Update your channel definition like this:
Broadcast::channel('admin.{id}', function ($admin, $id) {
return Auth::guard('admin')->check() && $admin->id === (int) $id;
});
### 5. **Emit Events for Admins**
Create an event that will be broadcasted. For example:
php artisan make:event AdminNotification
In the `AdminNotification` class:
namespace AppEvents;
use IlluminateBroadcastingChannel;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateBroadcastingPresenceChannel;
use IlluminateBroadcastingPrivateChannel;
use IlluminateContractsBroadcastingShouldBroadcast;
use IlluminateQueueSerializesModels;
class AdminNotification implements ShouldBroadcast
{
use InteractsWithSockets, SerializesModels;
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function broadcastOn()
{
return new PrivateChannel('admin.' . auth()->id());
}
}
### 6. **Frontend (Laravel Echo)**
Set up Laravel Echo on the frontend to listen to the private channel for the `admin`:
import Echo from "laravel-echo";
window.Pusher = require("pusher-js");
window.Echo = new Echo({
broadcaster: "pusher",
key: "your-pusher-key",
cluster: "your-pusher-cluster",
encrypted: true,
});
window.Echo.private(`admin.${adminId}`)
.listen('AdminNotification', (e) => {
console.log('Notification:', e.message);
});
Make sure `adminId` corresponds to the currently authenticated admin's ID.
### 7. **Test the Setup**
1. Trigger the event for the authenticated admin:
use AppEventsAdminNotification;
event(new AdminNotification('This is a message for the admin!'));
2. Check the frontend console to see if the message is received in real-time.
This setup ensures that the private broadcast channel is scoped to a specific `admin` user, secured via the `admin` guard.