Real-Time Mastery with Laravel Reverb
The Reverb Revolution
Installation and Core Setup
Getting started requires just a single command. Run php artisan install:broadcast to kick off the process. This command is a powerhouse; it publishes your configuration files, creates the routes/channels.php file, and installs reverb.php config file and updated environment variables for your ID, Key, and Secret.
Backend Logic: Events and Channels
To push data, you must define an Event that implements the ShouldBroadcast interface. Use the broadcastOn method to specify your channel.
public function broadcastOn(): array
{
return [
new PrivateChannel('orders.' . $this->order->id),
];
}
Start your server with php artisan reverb:start. This spins up the engine that listens for these dispatched events and pushes them to connected clients.
Frontend Integration with Echo
On the client side,
Echo.private(`orders.${this.orderId}`)
.listen('OrderShipmentStatusUpdate', (e) => {
this.status = e.status;
this.updateProgressBar();
});
Securing Data via Private Channels
Security is non-negotiable. While public channels work for general updates, order statuses require routes/channels.php. This ensures a user can only listen to updates for orders they actually own, preventing data leaks across your application.
