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.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.
