Event Broadcasting with Pusher and Laravel Echo
Event broadcasting lets server-side events push real-time updates to connected browsers, without the client needing to poll for changes.
The Pieces Involved
| Piece | Role |
|---|---|
| Pusher | Hosted WebSocket service that relays broadcast events |
| Laravel Echo | JavaScript library that subscribes to channels in the browser |
| ShouldBroadcast | Interface marking a Laravel event as broadcastable |
Making an Event Broadcastable
class OrderShipped implements ShouldBroadcast
{
public function broadcastOn(): Channel
{
return new Channel('orders');
}
}
Listening in the Browser
Echo.channel('orders')
.listen('OrderShipped', (e) => {
console.log(e.order);
});
Private and Presence Channels
Beyond public channels, Laravel supports private channels (authorized per user) and presence channels (which also track who's currently subscribed) for more controlled real-time features.
Broadcasting covers real-time communication. The next set of topics shifts to code organization patterns that keep larger Laravel applications maintainable.
Setting Up Broadcasting Step by Step
The exact commands differ slightly between Laravel versions, so check the documentation for your version. A typical setup on recent releases looks like this:
- Run
php artisan install:broadcasting, which createsroutes/channels.php, a broadcasting config file and an Echo setup file. Older versions instead required uncommenting the broadcast service provider. - Install the client libraries with
npm install laravel-echo pusher-js. - Add your Pusher app credentials to
.env. - Make sure a queue worker is running, because broadcast events are queued by default.
BROADCAST_CONNECTION=pusher
PUSHER_APP_ID=your-app-id
PUSHER_APP_KEY=your-app-key
PUSHER_APP_SECRET=your-app-secret
PUSHER_APP_CLUSTER=your-cluster
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
Older projects use BROADCAST_DRIVER instead of BROADCAST_CONNECTION. Only the key and cluster go to the browser; the secret must stay on the server.
Once everything is wired up, a single broadcast travels like this: your code calls OrderShipped::dispatch($order), Laravel places the job on the queue, a worker sends the payload to Pusher over HTTP, Pusher pushes it through open WebSocket connections to every subscribed browser, and Echo runs your JavaScript callback. If any link in that chain is missing, the message silently never arrives, which is why the queue worker is the first thing to check.
Controlling the Payload and Channel Access
By default every public property of the event is sent. You can shape the data and the event name yourself:
class OrderShipped implements ShouldBroadcast
{
public function __construct(public Order $order) {}
public function broadcastOn(): PrivateChannel
{
return new PrivateChannel('orders.' . $this->order->user_id);
}
public function broadcastAs(): string
{
return 'order.shipped';
}
public function broadcastWith(): array
{
return ['id' => $this->order->id, 'status' => 'shipped'];
}
}
A private channel needs an authorization rule in routes/channels.php. Echo calls this rule automatically before subscribing:
Broadcast::channel('orders.{userId}', function ($user, $userId) {
return (int) $user->id === (int) $userId;
});
In the browser, listen on the private channel. A leading dot tells Echo the name came from broadcastAs:
Echo.private(`orders.${userId}`)
.listen('.order.shipped', (e) => {
showToast(`Order ${e.id} has shipped`);
});
Choosing a Delivery Option
| Option | When it suits |
|---|---|
| Pusher Channels | Hosted service, no WebSocket server to run |
| Laravel Reverb | Self-hosted WebSocket server maintained by the Laravel team |
| Ably | Another hosted service with a Pusher-compatible mode |
| Polling | Very simple pages where a short delay is acceptable |
Because Echo talks to all of these through the same API, switching later mostly means changing configuration rather than rewriting listeners.
Troubleshooting Checklist
- Nothing arrives: check that a queue worker is running, or implement
ShouldBroadcastNowto skip the queue while debugging. - 403 on subscribe: the channel rule returned false, or the user is not logged in.
- Listener never fires: the event name does not match; remember the leading dot for custom names.
- Sender sees its own update twice: call
broadcast(new OrderShipped($order))->toOthers(). - Sensitive data leaking: use
broadcastWith()to send only the fields the browser needs.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.