LaraflareDocsFrameworkPackagesReleases13.x

Docs / Packages / Reverb

Reverb

Introduction

Reverb brings blazing-fast and scalable real-time WebSocket communication directly to your laraflare application, and provides seamless integration with laraflare's existing suite of event broadcasting tools.

Note

Reverb is a long-running WebSocket server, which a Worker cannot be. Run it on a machine of your own; your application reaches it over HTTP, as it reaches Pusher. To keep everything on Cloudflare, use the Durable Objects broadcast driver instead.

Installation

You may install Reverb using the install:broadcasting Artisan command:

npx artisan install:broadcasting

Configuration

Behind the scenes, the install:broadcasting Artisan command will install the @laraflare/reverb package and run the reverb:install command, which will install Reverb with a sensible set of default configuration options. If you would like to make any configuration changes, you may do so by updating Reverb's environment variables or by updating the config/reverb.ts configuration file.

Application Credentials

In order to establish a connection to Reverb, a set of Reverb "application" credentials must be exchanged between the client and server. These credentials are configured on the server and are used to verify the request from the client. You may define these credentials using the following environment variables:

REVERB_APP_ID=my-app-id
REVERB_APP_KEY=my-app-key
REVERB_APP_SECRET=my-app-secret

Allowed Origins

You may also define the origins from which client requests may originate by updating the value of the allowed_origins configuration value within the apps section of the config/reverb.ts configuration file. Any requests from an origin not listed in your allowed origins will be rejected. You may allow all origins using *:

apps: {
    provider: 'config',

    apps: [
        {
            app_id: 'my-app-id',
            allowed_origins: ['example.com'],
            // ...
        },
    ],
},

Additional Applications

Typically, Reverb provides a WebSocket server for the application in which it is installed. However, it is possible to serve more than one application using a single Reverb installation.

For example, you may wish to maintain a single laraflare application which, via Reverb, provides WebSocket connectivity for multiple applications. This can be achieved by defining multiple apps in your application's config/reverb.ts configuration file:

apps: {
    provider: 'config',

    apps: [
        {
            app_id: 'my-app-one',
            // ...
        },
        {
            app_id: 'my-app-two',
            // ...
        },
    ],
},

SSL

In most cases, secure WebSocket connections are handled by the upstream web server (Nginx, etc.) before the request is proxied to your Reverb server.

However, it can sometimes be useful, such as during local development, for the Reverb server to handle secure connections directly. If you are using Valet and have run the secure command against your application, or you are using Herd's secure site feature, you may use the certificate generated for your site to secure your Reverb connections. To do so, set the REVERB_HOST environment variable to your site's hostname or explicitly pass the hostname option when starting the Reverb server:

npx artisan reverb:start --host="0.0.0.0" --port=8080 --hostname="my-app.test"

Since Herd and Valet domains resolve to localhost, running the command above will result in your Reverb server being accessible via the secure WebSocket protocol (wss) at wss://my-app.test:8080.

You may also manually choose a certificate by defining tls options in your application's config/reverb.ts configuration file. Within the tls options, you may provide the certificate with local_cert and its key with local_pk:

options: {
    tls: {
        local_cert: '/path/to/cert.pem',
        local_pk: '/path/to/key.pem',
    },
},

Running the Server

The Reverb server can be started using the reverb:start Artisan command:

npx artisan reverb:start

By default, the Reverb server will be started at 0.0.0.0:8080, making it accessible from all network interfaces.

If you need to specify a custom host or port, you may do so via the --host and --port options when starting the server:

npx artisan reverb:start --host=127.0.0.1 --port=9000

Alternatively, you may define REVERB_SERVER_HOST and REVERB_SERVER_PORT environment variables in your application's .env configuration file.

The REVERB_SERVER_HOST and REVERB_SERVER_PORT environment variables should not be confused with REVERB_HOST and REVERB_PORT. The former specify the host and port on which to run the Reverb server itself, while the latter pair instruct laraflare where to send broadcast messages. For example, in a production environment, you may route requests from your public Reverb hostname on port 443 to a Reverb server operating on 0.0.0.0:8080. In this scenario, your environment variables would be defined as follows:

REVERB_SERVER_HOST=0.0.0.0
REVERB_SERVER_PORT=8080

REVERB_HOST=ws.example.com
REVERB_PORT=443

Debugging

To improve performance, Reverb does not output any debug information by default. If you would like to see the stream of data passing through your Reverb server, you may provide the --debug option to the reverb:start command:

npx artisan reverb:start --debug

Restarting

Since Reverb is a long-running process, changes to your code will not be reflected without restarting the server via the reverb:restart Artisan command.

The reverb:restart command ensures all connections are gracefully terminated before stopping the server. If you are running Reverb with a process manager such as Supervisor, the server will be automatically restarted by the process manager after all connections have been terminated:

npx artisan reverb:restart

Monitoring

Reverb may be monitored via an integration with Pulse. By enabling Reverb's Pulse integration, you may track the number of connections and messages being handled by your server.

To enable the integration, you should first ensure you have installed Pulse. Then, add any of Reverb's recorders to your application's config/pulse.ts configuration file:

import { ReverbConnections } from '@laraflare/reverb/Pulse/Recorders/ReverbConnections';
import { ReverbMessages } from '@laraflare/reverb/Pulse/Recorders/ReverbMessages';

recorders: new Map([
    [ReverbConnections, {
        sample_rate: 1,
    }],

    [ReverbMessages, {
        sample_rate: 1,
    }],

    // ...
]),

Note

Pulse's dashboard cards are server-rendered components, and laraflare's screens are Vue, so there are no Reverb cards to add to a dashboard: the recorders aggregate the data, and you may show it on your own Vue screens.

Connection activity is recorded by polling for new updates on a periodic basis. To ensure this information is recorded correctly, you must run the pulse:check daemon on your Reverb server. If you are running Reverb in a horizontally scaled configuration, you should only run this daemon on one of your servers.

Running Reverb in Production

Due to the long-running nature of WebSocket servers, you may need to make some optimizations to your server and hosting environment to ensure your Reverb server can effectively handle the optimal number of connections for the resources available on your server.

Open Files

Each WebSocket connection is held in memory until either the client or server disconnects. In Unix and Unix-like environments, each connection is represented by a file. However, there are often limits on the number of allowed open files at both the operating system and application level.

Operating System

On a Unix based operating system, you may determine the allowed number of open files using the ulimit command:

ulimit -n

This command will display the open file limits allowed for different users. You may update these values by editing the /etc/security/limits.conf file. For example, updating the maximum number of open files to 10,000 for the deploy user would look like the following:

# /etc/security/limits.conf
deploy        soft  nofile  10000
deploy        hard  nofile  10000

Event Loop

Under the hood, Reverb runs on Node's event loop to manage WebSocket connections on the server. It needs no additional extensions and has no event loop of its own to swap, so there is nothing to install to handle more than 1,000 concurrent connections. You are still limited by the open file limits described above.

Web Server

In most cases, Reverb runs on a non web-facing port on your server. So, in order to route traffic to Reverb, you should configure a reverse proxy. Assuming Reverb is running on host 0.0.0.0 and port 8080 and your server utilizes the Nginx web server, a reverse proxy can be defined for your Reverb server using the following Nginx site configuration:

server {
    ...

    location / {
        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;
    }

    ...
}

Warning

Reverb listens for WebSocket connections at /app and handles API requests at /apps. You should ensure the web server handling Reverb requests can serve both of these URIs.

Typically, web servers are configured to limit the number of allowed connections in order to prevent overloading the server. To increase the number of allowed connections on an Nginx web server to 10,000, the worker_rlimit_nofile and worker_connections values of the nginx.conf file should be updated:

user deploy;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
worker_rlimit_nofile 10000;

events {
  worker_connections 10000;
  multi_accept on;
}

The configuration above will allow up to 10,000 Nginx workers per process to be spawned. In addition, this configuration sets Nginx's open file limit to 10,000.

Ports

Unix-based operating systems typically limit the number of ports which can be opened on the server. You may see the current allowed range via the following command:

cat /proc/sys/net/ipv4/ip_local_port_range
# 32768	60999

The output above shows the server can handle a maximum of 28,231 (60,999 - 32,768) connections since each connection requires a free port. Although we recommend horizontal scaling to increase the number of allowed connections, you may increase the number of available open ports by updating the allowed port range in your server's /etc/sysctl.conf configuration file.

Process Management

In most cases, you should use a process manager such as Supervisor to ensure the Reverb server is continually running. If you are using Supervisor to run Reverb, you should update the minfds setting of your server's supervisor.conf file to ensure Supervisor is able to open the files required to handle connections to your Reverb server:

[supervisord]
...
minfds=10000

Scaling

If you need to handle more connections than a single server will allow, you may scale your Reverb server horizontally. Utilizing the publish / subscribe capabilities of Redis, Reverb is able to manage connections across multiple servers. When a message is received by one of your application's Reverb servers, the server will use Redis to publish the incoming message to all other servers.

To enable horizontal scaling, you should set the REVERB_SCALING_ENABLED environment variable to true in your application's .env configuration file:

REVERB_SCALING_ENABLED=true

Next, you should have a dedicated, central Redis server to which all of the Reverb servers will communicate. Reverb reads the Redis connection from the scaling.server section of config/reverb.ts, which takes its values from the same REDIS_* environment variables as the default Redis connection.

Once you have enabled Reverb's scaling option and configured a Redis server, you may simply invoke the reverb:start command on multiple servers that are able to communicate with your Redis server. These Reverb servers should be placed behind a load balancer that distributes incoming requests evenly among the servers.

Events

Reverb dispatches internal events during the lifecycle of a connection and message handling. You may listen for these events to perform actions when connections are managed or messages are exchanged.

The following events are dispatched by Reverb, and each is exported from @laraflare/reverb:

ChannelCreated

Dispatched when a channel is created. This typically occurs when the first connection subscribes to a specific channel. The event receives the Channel instance in its channel property.

ChannelRemoved

Dispatched when a channel is removed. This typically occurs when the last connection unsubscribes from a channel. The event receives the Channel instance in its channel property.

ConnectionPruned

Dispatched when a stale connection is pruned by the server. The event receives the Connection instance in its connection property.

MessageReceived

Dispatched when a message is received from a client connection. The event receives the Connection instance in its connection property and the raw string message.

MessageSent

Dispatched when a message is sent to a client connection. The event receives the Connection instance in its connection property and the raw string message.

Adapted from Laravel's documentation, Copyright (c) Taylor Otwell, used under the MIT License.