All posts

Laravel Reverb in Production: A Process That Never Stops

Reverb is not a regular PHP request but a long-running process. What that means for Supervisor, the reverse proxy, connection limits and deploys.

Marijan Kopčić••2 min read
Laravel Reverb in Production: A Process That Never Stops

A classic PHP request lives for a few dozen milliseconds. Reverb is a WebSocket server: a single PHP process that starts once and keeps connections open to all clients. Different rules apply to it.

Running it with Supervisor

Something has to keep php artisan reverb:start alive.

[program:reverb]
command=php /var/www/app/artisan reverb:start --host=127.0.0.1 --port=8080
user=www-data
autostart=true
autorestart=true
stopwaitsecs=10

Reverse proxy

Reverb listens locally, and the outside world reaches it through Nginx, which handles TLS and upgrades the connection to WebSocket:

location / {
    proxy_http_version 1.1;
    proxy_set_header Host $http_host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_pass http://127.0.0.1:8080;
}

Connection limits

Every WebSocket connection is one open file. Three places limit the number of connections:

  • the open file limit for the user (ulimit -n) and minfds in Supervisor
  • worker_connections in Nginx
  • PHP's default event loop, which cannot watch more than 1024 open files. Beyond that you need the ext-uv extension.

Deploy

Reverb keeps the code in memory, so it does not see changes until it is restarted:

php artisan reverb:restart

The same goes for queue workers and queue:restart. OPcache and reloading FPM do not help here, because Reverb does not go through FPM.

Memory

In a long-running process, memory that is not freed stays taken until the restart. It is worth watching the process's memory use and not collecting data in static properties and singletons.

Keep reading