Building Real-Time Features with Laravel and WebSockets
A practical guide to adding live notifications, collaborative editing, and real-time dashboards to Laravel apps — without the complexity of a Node.js backend.
Real-time features used to mean adding a Node.js server alongside your Laravel app. That's changed. With Laravel Reverb (or the self-hosted laravel-websockets package) you can handle WebSocket connections natively in PHP without any additional infrastructure. Here's how I approach it in production.
The Stack
- Laravel Reverb (or laravel-websockets) — WebSocket server written in PHP
- Laravel Echo — JavaScript client that abstracts socket subscriptions
- Laravel Events + Broadcasting — server-side event dispatch
- Queues — so broadcasting doesn't block request lifecycle
Broadcasting an Event
The key is keeping the HTTP request fast. Dispatch your event to a queue, let the queue worker broadcast it asynchronously. This prevents WebSocket latency from impacting your API response times.
class OrderStatusUpdated implements ShouldBroadcastNow
{
public function __construct(public Order $order) {}
public function broadcastOn(): array
{
return [new PrivateChannel("orders.{$this->order->user_id}")];
}
public function broadcastWith(): array
{
return ['status' => $this->order->status, 'id' => $this->order->id];
}
}Listening on the Frontend
window.Echo.private(`orders.${userId}`)
.listen('OrderStatusUpdated', (e: { id: number; status: string }) => {
updateOrderInList(e.id, e.status);
});Channel Authorization
Private channels require the client to authenticate before subscribing. Laravel handles this through a broadcast auth route that you define in your routes/channels.php file. This is where you put your authorization logic — ensuring users can only subscribe to their own data channels.
Production Considerations
- Run Reverb behind a reverse proxy (nginx) to handle SSL termination
- Use Supervisor to keep the Reverb process alive
- Monitor WebSocket connections with Laravel Telescope or custom metrics
- Set reconnection strategies on the client to handle network drops gracefully
The Daleel e-government platform I built uses this exact pattern for real-time application status updates. When a government officer approves a business registration, the applicant's dashboard updates instantly without polling. Keeping the stack homogeneous — PHP all the way through — means fewer moving parts and easier deployment.