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.