Broadcasting adds real-time capabilities to Laravel apps by allowing events to be broadcast to clients via WebSocket. It works with services like Pusher and Redis to provide functionality such as notifications, chat systems, and live updates.
Broadcasting was added in Laravel for real-time communication. It combines server-side events with front-end frameworks.
Install a WebSocket driver (e.g., Pusher):
composer require pusher/pusher-php-server
Configure broadcasting in your .env
file:
BROADCAST_DRIVER=pusher
Broadcast an event:
use App\Events\OrderShipped;
OrderShipped::dispatch($order);
Listen to the event on the front-end using Laravel Echo:
Echo.channel('orders')
.listen('OrderShipped', (e) => {
console.log(e.order);
});