DevOps

/laravel-agent:reverb:setup

Setup Laravel Reverb WebSocket server

Overview

The /reverb:setup command configures Laravel Reverb, a first-party WebSocket server for Laravel applications. It handles complete setup including server installation, environment configuration, frontend Echo client setup, and example channel creation for real-time features like live notifications, chat, and presence channels.

Usage

/laravel-agent:reverb:setup [options]

Examples

# Full setup with Laravel Echo and frontend integration
/laravel-agent:reverb:setup

# Minimal setup - server only, no frontend configuration
/laravel-agent:reverb:setup minimal

# Scaled setup with Redis for horizontal scaling
/laravel-agent:reverb:setup scaled

Setup Options

Choose the setup mode that fits your deployment needs:

  • Default (Full Setup) - Installs server, configures environment, sets up Laravel Echo client, and creates example channels
  • Minimal - Server-only installation without frontend integration, useful for API-only applications
  • Scaled - Includes Redis configuration for horizontal scaling across multiple Reverb instances

Installation Process

The command performs the following steps:

1. Package Installation

Installs Laravel Reverb server and frontend dependencies:

composer require laravel/reverb
php artisan reverb:install
npm install --save-dev laravel-echo pusher-js

2. Environment Configuration

Configures your .env file with required variables:

BROADCAST_CONNECTION=reverb

# Server Configuration
REVERB_APP_ID=generated-app-id
REVERB_APP_KEY=generated-app-key
REVERB_APP_SECRET=generated-app-secret
REVERB_HOST=localhost
REVERB_PORT=8080
REVERB_SCHEME=http

# Frontend Configuration
VITE_REVERB_APP_KEY="${REVERB_APP_KEY}"
VITE_REVERB_HOST="${REVERB_HOST}"
VITE_REVERB_PORT="${REVERB_PORT}"
VITE_REVERB_SCHEME="${REVERB_SCHEME}"

3. Laravel Echo Setup

Creates and configures the Echo client in resources/js/echo.js:

import Echo from 'laravel-echo';
import Pusher from 'pusher-js';

window.Pusher = Pusher;

window.Echo = new Echo({
    broadcaster: 'reverb',
    key: import.meta.env.VITE_REVERB_APP_KEY,
    wsHost: import.meta.env.VITE_REVERB_HOST,
    wsPort: import.meta.env.VITE_REVERB_PORT,
    wssPort: import.meta.env.VITE_REVERB_PORT,
    forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https',
    enabledTransports: ['ws', 'wss'],
});

The command also imports this file in your main resources/js/app.js:

import './echo';

4. Example Channel Creation

Sets up a sample broadcast event and channel authorization in routes/channels.php:

<?php

use Illuminate\Support\Facades\Broadcast;

Broadcast::channel('notifications.{userId}', function ($user, $userId) {
    return (int) $user->id === (int) $userId;
});

What Gets Created

A complete Reverb setup includes the following components:

Component Location Description
Reverb Server config/reverb.php WebSocket server configuration
Broadcasting Config config/broadcasting.php Updated with Reverb connection
Echo Client resources/js/echo.js Frontend WebSocket client configuration
Channel Routes routes/channels.php Channel authorization logic
Environment Variables .env Server and client configuration
Example Event app/Events/ Sample broadcast event for testing

Starting the Server

After setup, start the Reverb WebSocket server:

# Start server in foreground
php artisan reverb:start

# Start with debug output
php artisan reverb:start --debug

# Start in background (Unix/Linux)
php artisan reverb:start &

Testing Your Setup

Verify your Reverb installation is working correctly:

Backend Test

# Start the server in a separate terminal
php artisan reverb:start

# Open tinker and broadcast an event
php artisan tinker
>>> broadcast(new App\Events\TestEvent('Hello WebSocket!'));

Frontend Test

Listen for events in your JavaScript:

// In your frontend code
window.Echo.channel('notifications.1')
    .listen('TestEvent', (e) => {
        console.log('Received:', e);
    });

Example Broadcast Event

Create a broadcast event class:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class TestEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public function __construct(
        public string $message
    ) {}

    public function broadcastOn(): array
    {
        return [
            new Channel('notifications.1'),
        ];
    }
}

Common Channel Types

Reverb supports three types of channels:

Public Channels

<?php

// No authorization required
new Channel('announcements');
// Listen on frontend
Echo.channel('announcements')
    .listen('AnnouncementMade', (e) => {
        console.log(e.announcement);
    });

Private Channels

<?php

// Requires user authentication
new PrivateChannel('orders.' . $orderId);
// Listen on frontend (requires auth)
Echo.private('orders.123')
    .listen('OrderShipped', (e) => {
        console.log(e.order);
    });

Presence Channels

<?php

// Track who's online
new PresenceChannel('chat.' . $roomId);
// Listen on frontend
Echo.join('chat.1')
    .here((users) => {
        console.log('Currently online:', users);
    })
    .joining((user) => {
        console.log(user.name + ' joined');
    })
    .leaving((user) => {
        console.log(user.name + ' left');
    });

Scaled Setup with Redis

For production deployments with multiple Reverb instances, use Redis scaling:

# Install Redis
composer require predis/predis

# Configure in config/reverb.php
'scaling' => [
    'enabled' => true,
    'channel' => env('REVERB_SCALING_CHANNEL', 'reverb'),
],

Production Deployment

Best practices for running Reverb in production:

  1. Use SSL/TLS - Set REVERB_SCHEME=https and configure your reverse proxy
  2. Process Manager - Use Supervisor or systemd to keep Reverb running
  3. Enable Scaling - Use Redis scaling for multiple app instances
  4. Monitor Connections - Track active connections and memory usage
  5. Set Connection Limits - Configure max_connections in reverb.php

Supervisor Configuration

[program:reverb]
command=/usr/bin/php /path/to/your/app/artisan reverb:start
autostart=true
autorestart=true
user=www-data
redirect_stderr=true
stdout_logfile=/var/log/reverb.log

Configuration Report

After successful setup, the command provides a comprehensive report:

## Reverb Configured

### Server
Start with: `php artisan reverb:start`

### Environment
- REVERB_APP_ID: xxx
- REVERB_APP_KEY: xxx
- WebSocket URL: ws://localhost:8080

### Client
Echo configured in resources/js/echo.js

### Test
```bash
php artisan reverb:start &
php artisan tinker
>>> broadcast(new App\Events\TestEvent('Hello!'))
```

Troubleshooting

  • Connection refused - Ensure Reverb server is running with php artisan reverb:start
  • 401 Unauthorized - Check channel authorization in routes/channels.php
  • CORS errors - Configure allowed origins in config/reverb.php
  • Messages not received - Verify VITE_ env variables match server config
  • Port conflicts - Change REVERB_PORT if 8080 is already in use

See Also