<?php
 use App\Models\Admin; use App\Models\ChatSession; use App\Models\Client; use Illuminate\Support\Facades\Broadcast; Broadcast::channel('App.Models.User.{id}', function ($user, $id) { return (int) $user->id === (int) $id; }); Broadcast::channel('chat.{sessionId}', function ($user, $sessionId) { $session = ChatSession::find($sessionId); if (! $session) { return false; } if ($user instanceof Admin) { if (! $user->can('view tickets')) { return false; } return ['id' => 'admin-' . $user->id, 'name' => $user->name ?? 'Admin']; } if ($user instanceof Client) { $activeClientId = (int) (session('active_client_id') ?? $user->id); if ((int) $session->client_id === $activeClientId) { return ['id' => 'client-' . $user->id, 'name' => $session->visitor_name]; } } return false; }, ['guards' => ['admin', 'client']]); Broadcast::channel('admin.chat', function ($user) { return $user instanceof Admin && $user->can('view tickets'); }, ['guards' => ['admin']]); 