Setup Laravel Reverb Server , Setup Env and Nginx Proxy

What is Laravel Reverb and when to use it

Laravel Reverb is Laravel’s first-party WebSocket server made for providing real-time broadcasting using the Pusher-compatible protocol. It lets you broadcast events from your server to connected clients without relying on third-party services. Reverb integrates with Laravel’s broadcasting stack so your existing events/listeners work with little change. 

Use Reverb when you want lower latency, full control over your real-time stack, or to avoid third-party costs. It replaces setups that used Pusher, soketi, socket.io or custom websockets in many Laravel projects.


Pre-requisites for a production Reverb server

Before you start:

  1. A Linux server (Ubuntu 20.04/22.04 recommended).
  2. PHP 8.x supported by your Laravel version, Composer, and required PHP extensions.
  3. Open ports: standard web (80/443) and an internal port for Reverb (commonly 6001,8080 or configurable). Reverb itself listens on its own port; Nginx will proxy to it.
  4. nginx installed and working with SSL (Let's Encrypt recommended or others).
  5. systemd or supervisor to keep the Reverb process alive.
  6. These are standard production items — make sure your server includes them.


Install and publish broadcasting support (Laravel provides installer helper):
cd /var/www/html/laravel-app
composer require laravel/reverb
php artisan install:broadcasting
Publish config if needed and inspect config/broadcasting.php to ensure reverb is available and configured as a driver.

The Laravel docs confirms Reverb is included as a first-party broadcasting driver and recommend using the provided artisan/install flows

Setup Nginx Server Configuration with Reverb Proxy

Here we have used let's encrypt certbot ssl certificate for ssl

map $http_upgrade $connection_upgrade {                                                                                     
    default upgrade;                                                                                                        
    ''      close;                                                                                                          
}                                                                                                                           
                                                                                                                            
server {                                                                                                                    
    server_name website.com www.website.com;                                                                            
    root /var/www/html/laravel-app/public;                                                                                          
                                                                                                                            
    add_header X-Frame-Options "SAMEORIGIN";                                                                                
    add_header X-XSS-Protection "1; mode=block";                                                                            
    add_header X-Content-Type-Options "nosniff";                                                                            
                                                                                                                            
    index index.html index.htm index.php;                                                                                   
    charset utf-8;                                                                                                          
                                                                                                                            
    location / {                                                                                                            
        try_files $uri $uri/ /index.php?$query_string;                                                                      
    }                                                                                                                       
                                                                                                                            
    # Reverb WebSocket proxy - THIS IS THE FIX                                                                              
    location /app {                                                                                                         
        proxy_http_version 1.1;                                                                                             
        proxy_set_header Host $http_host;                                                                                   
        proxy_set_header Scheme $scheme;                                                                                    
        proxy_set_header SERVER_PORT $server_port;                                                                          
        proxy_set_header REMOTE_ADDR $remote_addr;                                                                          
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                                                        
        proxy_set_header Upgrade $http_upgrade;                                                                             
        proxy_set_header Connection "Upgrade";                                                                              
                                                                                                                            
        proxy_pass http://0.0.0.0:8080;                                                                                     
    }                                                                                                                       
                                                                                                                            
    location = /favicon.ico { access_log off; log_not_found off; }                                                          
    location = /robots.txt  { access_log off; log_not_found off; }                                                          
                                                                                                                            
    error_page 404 /index.php;                                                                                              
                                                                                                                            
    location ~ \.php$ {                                                                                                     
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;                                                                     
        fastcgi_index index.php;                                                                                            
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;                                                   
        include fastcgi_params;                                                                                             
    }                                                                                                                       
                                                                                                                            
    location ~ /\.(?!well-known).* {                                                                                        
        deny all;                                                                                                           
    }                                                                                                                        
                                                                                                                             
    listen 443 ssl; # managed by Certbot                                                                                    
    ssl_certificate /etc/letsencrypt/live/website.com/fullchain.pem; # managed by Certbot                                 
    ssl_certificate_key /etc/letsencrypt/live/website.com/privkey.pem; # managed by Certbot                               
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot                                                   
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot                                                                                                                
}                                                                                                                           
                                                                                                                            
                                                                                                                            
                                                                                                                            
server {                                                                                                                    
    if ($host = www.website.com) {                                                                                        
        return 301 https://$host$request_uri;                                                                               
    } # managed by Certbot                                                                                                  
                                                                                                                            
                                                                                                                            
    if ($host = website.com) {                                                                                            
        return 301 https://$host$request_uri;                                                                               
    } # managed by Certbot                                                                                                  
                                                                                                                                                                                                                                             
    server_name website.com www.website.com;                                                                            
    listen 80;                                                                                                              
    return 404; # managed by Certbot                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                                         
}

Env file Reverb Configuration

## Reverb Start
REVERB_APP_ID=163456 #change according to your wish
REVERB_APP_KEY=i7kggvgihjxxxkdmster #change according to your wish
REVERB_APP_SECRET=xhm09feje245rcqp2g #change according to your wish

#where reverb server runs(used by artisan reverb:start)
REVERB_HOST="website.com"
REVERB_PORT=443
REVERB_SCHEME=https
REVERB_SERVER_SCHEME=http
VITE_REVERB_APP_KEY="${REVERB_APP_KEY}"
VITE_REVERB_APP_HOST="${REVERB_HOST}"
VITE_REVERB_APP_PORT="${REVERB_PORT}"
VITE_REVERB_APP_SCHEME=https
VITE_REVERB_APP_CLUSTER="${REVERB_APP_CLUSTER}"
## Reverb Config End

Keep Reverb Server Running using Supervisor

You need to keep reverb server running in background using supervisor. Supervisor installation process can be found here 

Create a file inside /etc/supervisor/conf.d/laravel-reverb.conf and save below configuration

[program:laravel-reverb]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/laravel-app/artisan reverb:start --port=8080
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/log/laravel-reverb.log

Now restart supervisor

You will connect to reverb server by below url. 

wss://website.com/app/i7kggvgihjxxxkdmster?protocol=7&client=js&version=8.4.0&flash=false

Test reverb socket connection by third party tools such as wscat

Hope this will help you. You may share this with others. 

Tags: