Laravel Reverb WebSockets – Guide to Real-Time Broadcasting

Laravel Reverb WebSockets

Introduction

Modern web applications require real-time communication for live updates, notifications, and chat applications. Laravel Reverb WebSockets is a first-party WebSocket server designed to work seamlessly with Laravel applications, removing the need for third-party services like Pusher or Socket.io.

Laravel Reverb WebSockets offers scalability, security, and full integration with Laravel’s broadcasting system and Laravel Echo. This guide covers its installation, setup, configuration, usage, and advanced features.

WebSockets allow bidirectional communication between the server and clients, ensuring seamless data exchange without polling the server frequently. Unlike traditional HTTP requests, WebSockets maintain a persistent connection, which makes them ideal for applications requiring real-time interactions.


Understanding WebSockets and Laravel Reverb

What Are WebSockets?

WebSockets are a full-duplex communication protocol that enables data exchange between a client and a server without needing repeated HTTP requests. This significantly reduces latency and enhances performance for applications requiring real-time updates, such as:

  • Chat applications
  • Live notifications
  • Multiplayer gaming
  • Stock market updates
  • Collaborative editing tools
  • IoT device communication

Laravel Reverb utilizes WebSockets to provide an efficient real-time broadcasting system tailored to Laravel applications.

Why Use Laravel Reverb Instead of Third-Party Services?

While third-party services like Pusher, Ably, and Firebase offer WebSocket-based real-time communication, they come with additional costs and dependencies. Laravel Reverb eliminates these limitations by offering:

  • No additional cost – You control the infrastructure.
  • Better performance – Optimized for Laravel applications.
  • Full control – Customize security, scalability, and behavior.
  • Seamless integration – Works with Laravel broadcasting and Echo out of the box.

With Laravel Reverb, you can build and scale real-time applications without worrying about vendor lock-in.


Installation and Setup

Step 1: Install Laravel Reverb

Run the following command to install Reverb:

php artisan install:broadcasting

This command installs Reverb and applies default settings.

Step 2: Configure Laravel Reverb

Modify config/reverb.php as needed:

return [
    'host' => env('REVERB_HOST', '0.0.0.0'),
    'port' => env('REVERB_PORT', 8080),
    'allowed_origins' => ['*'],
    'ssl' => [
        'enable' => env('REVERB_SSL', false),
        'cert' => env('REVERB_SSL_CERT', ''),
        'key' => env('REVERB_SSL_KEY', ''),
    ],
];

Update your .env file:

REVERB_HOST=0.0.0.0
REVERB_PORT=8080
REVERB_SSL=false

Step 3: Start Laravel Reverb

Run the WebSocket server:

php artisan reverb:start

To specify a host and port:

php artisan reverb:start --host=127.0.0.1 --port=6001

Step 4: Enable Broadcasting

Edit config/broadcasting.php:

'default' => env('BROADCAST_DRIVER', 'reverb'),

Update .env:

BROADCAST_DRIVER=reverb

Restart Reverb:

php artisan reverb:restart

Using Laravel Reverb for Real-Time Broadcasting

Creating an Event

Generate an event:

php artisan make:event OrderShipped

Modify app/Events/OrderShipped.php:

namespace App\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class OrderShipped
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $order;

    public function __construct($order)
    {
        $this->order = $order;
    }

    public function broadcastOn()
    {
        return ['orders'];
    }
}

Broadcasting Events

Trigger the event:

use App\Events\OrderShipped;
use App\Models\Order;

$order = Order::find(1);
event(new OrderShipped($order));

Listening to Events in Laravel Echo

Install Laravel Echo:

npm install --save laravel-echo pusher-js

Configure Echo in resources/js/bootstrap.js:

import Echo from 'laravel-echo';

window.Echo = new Echo({
    broadcaster: 'reverb',
    host: window.location.hostname + ':8080'
});

Listen for events:

window.Echo.channel('orders')
    .listen('OrderShipped', (e) => {
        console.log('Order shipped:', e.order);
    });

Compile assets:

npm run dev

Monitoring Laravel Reverb

Laravel Reverb integrates with Laravel Pulse for tracking performance. Enable it in config/pulse.php:

return [
    'reverb' => true,
];

Monitor Reverb:

php artisan pulse:monitor

Scaling and Performance Optimization

  • Use Redis for event distribution across multiple servers.
  • Enable SSL to secure WebSocket communication.
  • Utilize Supervisor to keep the Reverb process running in production.
  • Optimize the WebSocket event loop for handling large numbers of connections.

Conclusion

Laravel Reverb WebSockets is a powerful solution for real-time communication in Laravel applications. It provides seamless scalability, security, and real-time broadcasting capabilities. Whether you’re building chat applications, live notifications, or interactive dashboards, Reverb simplifies real-time event handling in Laravel.

By following this guide, you can set up, deploy, and optimize Laravel Reverb WebSockets efficiently for real-time application development.

You can also explore Laravel 12 New Features & Updates (2025) – Full Breakdown.

Leave a Reply

Your email address will not be published. Required fields are marked *