Nâng CaoLaravel iconLaravel

Broadcasting trong Laravel là gì và khi nào nên dùng?

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 OrderChannel xá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.

Xem toàn bộ Laravel cùng filter theo level & chủ đề con.

Mở danh sách Laravel