illuminate/broadcasting
Illuminate Broadcasting provides Laravel’s broadcasting layer for sending real-time events over channels via drivers like Pusher, Ably, and Redis. It includes broadcaster contracts, channel authorization, and event broadcasting utilities for scalable pub/sub.
Begin by ensuring the package is installed (it ships with Laravel by default). Configure broadcasting in config/broadcasting.php—set your default driver (e.g., pusher, redis, or log for dev) and define connection details. Next, create a BroadcastServiceProvider (if missing) and register channels in its boot() method. For immediate feedback, define a simple event implementing ShouldBroadcast, implement broadcastOn() (e.g., new PrivateChannel('user.1')), dispatch it using event(), and watch logs or browser DevTools for broadcast traffic. The log driver is ideal for initial testing to confirm payload structure and channel routing before moving to a production-grade driver.
OrderShipped, CommentPosted) as ShouldBroadcast events—keep payload lean (IDs, minimal metadata) to avoid bloat.Broadcast::channel() rules in routes/channels.php for granular access (e.g., return $user->id === $id; for private channels). For presence channels, return user metadata (name, ID) to populate join/leave lists.Echo.private('orders.{id}')—and bind to event names matching your class (e.g., OrderShipped → order.shipped). Use this. binding in Vue/React to avoid this context loss.queue connection in broadcasting.php and ensure workers (php artisan queue:work) are running to prevent message loss.Broadcast::event() for non-event broadcasting (e.g., ad-hoc admin alerts) and Event::dispatchNow() for synchronous tests.Auth::guard('web') matches your session guard—Auth::guard('api') won’t resolve Auth::user() for web-based sessions. Add @php echo Auth::check() ? 'logged in' : 'guest'; @endphp in a Blade view to debug auth state.php artisan queue:work redis). Miss this, and events vanish into the void.UserRegistered → User.Registered), but custom event names break Echo bindings. Override broadcastAs() if you need exact event names.ERR_RESPONSE_HEADERS_TOO_BIG or timeouts; compress or minimize broadcast data. For local dev with Pusher/Ably, set PUSHER_APP_HOST and PUSHER_APP_PORT to 127.0.0.1:8080 if proxying, and confirm frontend key/cluster match dashboard.'log' => ['driver' => 'log'] in broadcasting.php—every broadcast attempt appears in laravel.log, including 403s and serialization errors. Use dd() inside broadcastOn() or authorization closures only in dev, as it halts queue processing.PusherBroadcaster or implement Broadcaster to inject auth tokens, retry logic, or custom headers—e.g., for custom auth proxies or load-balanced broadcast servers.How can I help you explore Laravel packages today?