Broadcasting gửi dữ liệu thời gian thực đến client kết nối qua WebSocket.
- Tạo channel:
php artisan make:channel OrderChannelxác định ai có thể lắng nghe. - Broadcast event trong controller:
broadcast(new OrderShipped($order)). - Frontend dùng Laravel Echo đăng ký:
Echo.channel("order." + orderId).listen("OrderShipped", (e) => { ... }). - Use case: thông báo (tin nhắn mới xuất hiện ngay), presence channel (xem ai đang online), collaborative editing, live dashboard.
- Yêu cầu server hỗ trợ WebSocket (Pusher, Ably, hoặc self-hosted Soketi).
- Broadcasting tạo ra trải nghiệm real-time thay vì polling lãng phí tài nguyên.
Broadcasting sends real-time data to connected clients via WebSockets.
- Create channel:
php artisan make:channel OrderChannelauthorizing who can listen. - Broadcast event in controller:
broadcast(new OrderShipped($order)). - Frontend with Laravel Echo subscribes:
Echo.channel("order." + orderId).listen("OrderShipped", (e) => { ... }). - Use cases: notifications (new message appears instantly), presence channels (show who's online), collaborative editing, live dashboards.
- Requires server supporting WebSockets (Pusher, Ably, or self-hosted Soketi).
- Broadcasting creates interactive real-time experiences vs. polling which wastes resources.