Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/DevOps & cloud

WebSockets with Laravel Reverb: Real-Time Apps Made Simple

Build real-time features — chat, notifications, live dashboards — using Laravel Reverb, the official first-party WebSocket server.

22 February 20261 min readDevOps & cloudاقرأ بالعربية

On this page

  1. Installation
  2. Broadcasting Events
  3. Frontend Listening (Laravel Echo)
  4. Channel Authorization
  5. Real-Time Notifications

Laravel Reverb is the official first-party WebSocket server, released with Laravel 11. It replaces Pusher, runs on your infrastructure, and handles 10,000+ concurrent connections.

Installation

bash
composer require laravel/reverb
php artisan reverb:install
php artisan reverb:start --port=8080

Broadcasting Events

php
class MessageSent implements ShouldBroadcast
{
    public function __construct(
        public User $user,
        public Message $message,
    ) {}

    public function broadcastOn(): array
    {
        return [new PrivateChannel("chat.{$this->message->room_id}")];
    }

    public function broadcastWith(): array
    {
        return [
            'user' => ['id' => $this->user->id, 'name' => $this->user->name],
            'message' => [
                'id' => $this->message->id,
                'body' => $this->message->body,
                'sent_at' => $this->message->created_at->toISOString(),
            ],
        ];
    }
}

MessageSent::dispatch($user, $message);

Frontend Listening (Laravel Echo)

javascript
Echo.private(`chat.${roomId}`)
    .listen('MessageSent', (event) => {
        messages.push(event.message);
    });

// Presence channel — who's online
Echo.join(`room.${roomId}`)
    .here((users) => { onlineUsers = users; })
    .joining((user) => { onlineUsers.push(user); })
    .leaving((user) => {
        onlineUsers = onlineUsers.filter(u => u.id !== user.id);
    });

Channel Authorization

php
Broadcast::channel('chat.{roomId}', function (User $user, int $roomId) {
    return $user->rooms()->where('id', $roomId)->exists();
});

Real-Time Notifications

php
class OrderShipped extends Notification implements ShouldBroadcast
{
    public function via($notifiable): array
    {
        return ['database', 'broadcast'];
    }

    public function toBroadcast($notifiable): BroadcastMessage
    {
        return new BroadcastMessage([
            'title' => 'Order Shipped!',
            'body' => "Order #{$this->order->id} has been shipped.",
        ]);
    }
}

Reverb gives you Pusher-like functionality for free, on your own server, with zero external dependencies.

All articles

Keep reading

  • Docker for Developers: From Zero to Containerized Applications28 February 20261 min read
  • CI/CD Pipelines with GitHub Actions: Automate Everything22 February 20261 min read
  • Clean Code Principles: Write Code That Humans Can Read22 February 20261 min read