Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 04ad014

Browse filesBrowse files
joedixonJubekitaylorotwelldwightwatson
authored
[10.x] Adds Reverb docs (#9388)
* add Reverb docs * wip * wip * wip * wip * typos Co-authored-by: Julius Kiekbusch <contact@julius-kiekbusch.de> * wip * update ssl support * update server variables * formatting * Update broadcasting.md * Update broadcasting.md Co-authored-by: Dwight Watson <dwightwatson@users.noreply.github.com> --------- Co-authored-by: Julius Kiekbusch <contact@julius-kiekbusch.de> Co-authored-by: Taylor Otwell <taylor@laravel.com> Co-authored-by: Dwight Watson <dwightwatson@users.noreply.github.com>
1 parent 6f7d982 commit 04ad014
Copy full SHA for 04ad014

File tree

Expand file treeCollapse file tree

3 files changed

+339
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+339
-1
lines changed

‎broadcasting.md

Copy file name to clipboardExpand all lines: broadcasting.md
+57-1Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
- [Introduction](#introduction)
44
- [Server Side Installation](#server-side-installation)
55
- [Configuration](#configuration)
6+
- [Reverb](#reverb)
67
- [Pusher Channels](#pusher-channels)
78
- [Ably](#ably)
89
- [Open Source Alternatives](#open-source-alternatives)
910
- [Client Side Installation](#client-side-installation)
11+
- [Reverb](#client-reverb)
1012
- [Pusher Channels](#client-pusher-channels)
1113
- [Ably](#client-ably)
1214
- [Concept Overview](#concept-overview)
@@ -52,7 +54,7 @@ The core concepts behind broadcasting are simple: clients connect to named chann
5254
<a name="supported-drivers"></a>
5355
#### Supported Drivers
5456

55-
By default, Laravel includes two server-side broadcasting drivers for you to choose from: [Pusher Channels](https://pusher.com/channels) and [Ably](https://ably.com). However, community driven packages such as [soketi](https://docs.soketi.app/) provide additional broadcasting drivers that do not require commercial broadcasting providers.
57+
By default, Laravel includes three server-side broadcasting drivers for you to choose from: [Laravel Reverb](https://reverb.laravel.com), [Pusher Channels](https://pusher.com/channels), and [Ably](https://ably.com).
5658

5759
> [!NOTE]
5860
> Before diving into event broadcasting, make sure you have read Laravel's documentation on [events and listeners](/docs/{{version}}/events).
@@ -79,6 +81,23 @@ Before broadcasting any events, you will first need to register the `App\Provide
7981

8082
You will also need to configure and run a [queue worker](/docs/{{version}}/queues). All event broadcasting is done via queued jobs so that the response time of your application is not seriously affected by events being broadcast.
8183

84+
<a name="reverb"></a>
85+
### Reverb
86+
87+
You may install Reverb using the Composer package manager. Since Reverb is currently in beta, you will need to explicitly install the beta release:
88+
89+
```sh
90+
composer require laravel/reverb:@beta
91+
```
92+
93+
Once the package is installed, you may run Reverb's installation command to publish the configuration, update your applications's broadcasting configuration, and add Reverb's required environment variables:
94+
95+
```sh
96+
php artisan reverb:install
97+
```
98+
99+
You can find detailed Reverb installation and usage instructions in the [Reverb documentation](/docs/{{version}}/reverb).
100+
82101
<a name="pusher-channels"></a>
83102
### Pusher Channels
84103

@@ -149,6 +168,43 @@ Finally, you are ready to install and configure [Laravel Echo](#client-side-inst
149168
<a name="client-side-installation"></a>
150169
## Client Side Installation
151170

171+
<a name="client-reverb"></a>
172+
### Reverb
173+
174+
[Laravel Echo](https://github.com/laravel/echo) is a JavaScript library that makes it painless to subscribe to channels and listen for events broadcast by your server-side broadcasting driver. You may install Echo via the NPM package manager. In this example, we will also install the `pusher-js` package since Reverb utilizes the Pusher protocol for WebSocket subscriptions, channels, and messages:
175+
176+
```shell
177+
npm install --save-dev laravel-echo pusher-js
178+
```
179+
180+
Once Echo is installed, you are ready to create a fresh Echo instance in your application's JavaScript. A great place to do this is at the bottom of the `resources/js/bootstrap.js` file that is included with the Laravel framework. By default, an example Echo configuration is already included in this file - you simply need to uncomment it and update the `broadcaster` configuration option to `reverb`:
181+
182+
```js
183+
import Echo from 'laravel-echo';
184+
185+
import Pusher from 'pusher-js';
186+
window.Pusher = Pusher;
187+
188+
window.Echo = new Echo({
189+
broadcaster: 'reverb',
190+
key: import.meta.env.VITE_REVERB_APP_KEY,
191+
wsHost: import.meta.env.VITE_REVERB_HOST,
192+
wsPort: import.meta.env.VITE_REVERB_PORT,
193+
wssPort: import.meta.env.VITE_REVERB_PORT,
194+
forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https',
195+
enabledTransports: ['ws', 'wss'],
196+
});
197+
```
198+
199+
Next, you should compile your application's assets:
200+
201+
```shell
202+
npm run build
203+
```
204+
205+
> [!WARNING]
206+
> The Laravel Echo `reverb` broadcaster requires laravel-echo v1.16.0+.
207+
152208
<a name="client-pusher-channels"></a>
153209
### Pusher Channels
154210

‎documentation.md

Copy file name to clipboardExpand all lines: documentation.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
- [Precognition](/docs/{{version}}/precognition)
9797
- [Prompts](/docs/{{version}}/prompts)
9898
- [Pulse](/docs/{{version}}/pulse)
99+
- [Reverb](/docs/{{version}}/reverb)
99100
- [Sail](/docs/{{version}}/sail)
100101
- [Sanctum](/docs/{{version}}/sanctum)
101102
- [Scout](/docs/{{version}}/scout)

‎reverb.md

Copy file name to clipboard
+281Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
# Laravel Reverb
2+
3+
- [Introduction](#introduction)
4+
- [Installation](#installation)
5+
- [Configuration](#configuration)
6+
- [Application Credentials](#application-credentials)
7+
- [Allowed Origins](#allowed-origins)
8+
- [Additional Applications](#additional-applications)
9+
- [SSL](#ssl)
10+
- [Running the Server](#running-server)
11+
- [Debugging](#debugging)
12+
- [Restarting](#restarting)
13+
- [Running Reverb in Production](#production)
14+
- [Open Files](#open-files)
15+
- [Event Loop](#event-loop)
16+
- [Web Server](#web-server)
17+
- [Ports](#ports)
18+
- [Process Management](#process-management)
19+
- [Scaling](#scaling)
20+
21+
<a name="introduction"></a>
22+
## Introduction
23+
24+
[Laravel Reverb](https://github.com/laravel/reverb) brings blazing-fast and scalable real-time WebSocket communication directly to your Laravel application, and provides seamless integration with Laravel’s existing suite of event broadcasting tools.
25+
26+
<a name="installation"></a>
27+
## Installation
28+
29+
> [!WARNING]
30+
> Laravel Reverb requires PHP 8.2+ and Laravel 10.47+.
31+
32+
You may use the Composer package manager to install Reverb into your Laravel project. Since Reverb is currently in beta, you will need to explicitly install the beta release:
33+
34+
```sh
35+
composer require laravel/reverb:@beta
36+
```
37+
38+
Once the package is installed, you may run Reverb's installation command to publish the configuration, add Reverb's required environment variables, and enable event broadcasting in your application:
39+
40+
```sh
41+
php artisan reverb:install
42+
```
43+
44+
<a name="configuration"></a>
45+
## Configuration
46+
47+
The `reverb:install` command will automatically configure Reverb using a sensible set of default 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.php` configuration file.
48+
49+
<a name="application-credentials"></a>
50+
### Application Credentials
51+
52+
In order 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:
53+
54+
```ini
55+
REVERB_APP_ID=my-app-id
56+
REVERB_APP_KEY=my-app-key
57+
REVERB_APP_SECRET=my-app-secret
58+
```
59+
60+
<a name="allowed-origins"></a>
61+
### Allowed Origins
62+
63+
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.php` configuration file. Any requests from an origin not listed in your allowed origins will be rejected. You may allow all origins using `*`:
64+
65+
```php
66+
'apps' => [
67+
[
68+
'id' => 'my-app-id',
69+
'allowed_origins' => ['laravel.com'],
70+
// ...
71+
]
72+
]
73+
```
74+
75+
<a name="additional-applications"></a>
76+
### Additional Applications
77+
78+
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.
79+
80+
For example, you may wish to maintain a single Laravel application which, via Reverb, provides WebSocket connectivity for multiple applications. This can be achieved by defining multiple `apps` in your application's `config/reverb.php` configuration file:
81+
82+
```php
83+
'apps' => [
84+
[
85+
'id' => 'my-app-one',
86+
// ...
87+
],
88+
[
89+
'id' => 'my-app-two',
90+
// ...
91+
],
92+
],
93+
```
94+
95+
<a name="ssl"></a>
96+
### SSL
97+
98+
In most cases, secure WebSocket connections are likely handled by an upstream web server (Nginx, etc.) before the request is proxied to your Reverb server.
99+
100+
However, it can sometimes be useful, such as during local development, for the Reverb server to handle secure connections directly. If you are using [Laravel Herd's](https://herd.laravel.com) secure site functionality, or you are using [Laravel Valet](/docs/{{version}}/valet) and have run the [secure command](/docs/{{version}}/valet#securing-sites) against your application, you may use the Herd / Valet 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:
101+
102+
```sh
103+
php artisan reverb:start --host="0.0.0.0" --port=8080 --hostname="laravel.test"
104+
```
105+
106+
Since Herd and Valet domains resolve to `localhost`, running the commmand above will result in your Reverb server being accessible via the secure WebSocket protocol (wss) at `wss://laravel.test:8080`.
107+
108+
You may also manually choose a certificate by defining `tls` options in your application's `config/reverb.php` configuration file. Within the array of `tls` options, you may provide any of the options supported by [PHP's SSL context options](https://www.php.net/manual/en/context.ssl.php):
109+
110+
```php
111+
'options' => [
112+
'tls' => [
113+
'local_cert' => '/path/to/cert.pem'
114+
],
115+
],
116+
```
117+
118+
<a name="running-server"></a>
119+
## Running the Server
120+
121+
The Reverb server can be started using the `reverb:start` Artisan command:
122+
123+
```sh
124+
php artisan reverb:start
125+
```
126+
127+
By default, the Reverb server will be started at `0.0.0.0:8080`, making it accessible from all network interfaces.
128+
129+
If you need to specify a custom host or port, you may do so via the `--host` and `--port` options when starting the server:
130+
131+
```sh
132+
php artisan reverb:start --host=127.0.0.1 --port=9000
133+
```
134+
135+
Alternatively, you may define `REVERB_SERVER_HOST` and `REVERB_SERVER_PORT` environment variables in your application's `.env` configuration file.
136+
137+
<a name="debugging"></a>
138+
### Debugging
139+
140+
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:
141+
142+
```sh
143+
php artisan reverb:start --debug
144+
```
145+
146+
<a name="restarting"></a>
147+
### Restarting
148+
149+
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.
150+
151+
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:
152+
153+
```sh
154+
php artisan reverb:restart
155+
```
156+
157+
<a name="production"></a>
158+
## Running Reverb in Production
159+
160+
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.
161+
162+
> [!NOTE]
163+
> If your site is managed by [Laravel Forge](https://forge.laravel.com), you may automatically optimize your server for Reverb directly from the "Application" panel. By enabling the Reverb integration, Forge will ensure your server is production-ready, including installing any required extensions and increasing the allowed number of connections.
164+
165+
<a name="open-files"></a>
166+
### Open Files
167+
168+
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.
169+
170+
<a name="operating-system"></a>
171+
#### Operating System
172+
173+
On a Unix based operating system, you may determine the allowed number of open files using the `ulimit` command:
174+
175+
```sh
176+
ulimit -n
177+
```
178+
179+
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 `forge` user would look like the following:
180+
181+
```ini
182+
# /etc/security/limits.conf
183+
forge soft nofile 10000
184+
forge hard nofile 10000
185+
```
186+
187+
<a name="event-loop"></a>
188+
### Event Loop
189+
190+
Under the hood, Reverb uses a ReactPHP event loop to manage WebSocket connections on the server. By default, this event loop is powered by `stream_select`, which doesn't require any additional extensions. However, `stream_select` is typically limited to 1,024 open files. As such, if you plan to handle more than 1,000 concurrent connections, you will need to use an alternative event loop not bound by the same restrictions.
191+
192+
Reverb will automatically switch to an `ext-event`, `ext-ev`, or `ext-uv` powered loop when available. All of these PHP extensions are available for install via PECL:
193+
194+
```sh
195+
pecl install event
196+
# or
197+
pecl install ev
198+
# or
199+
pecl install uv
200+
```
201+
202+
<a name="web-server"></a>
203+
### Web Server
204+
205+
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:
206+
207+
```nginx
208+
server {
209+
...
210+
211+
location / {
212+
proxy_http_version 1.1;
213+
proxy_set_header Host $http_host;
214+
proxy_set_header Scheme $scheme;
215+
proxy_set_header SERVER_PORT $server_port;
216+
proxy_set_header REMOTE_ADDR $remote_addr;
217+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
218+
proxy_set_header Upgrade $http_upgrade;
219+
proxy_set_header Connection "Upgrade";
220+
221+
proxy_pass http://0.0.0.0:8080;
222+
}
223+
224+
...
225+
}
226+
```
227+
228+
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:
229+
230+
```nginx
231+
user forge;
232+
worker_processes auto;
233+
pid /run/nginx.pid;
234+
include /etc/nginx/modules-enabled/*.conf;
235+
worker_rlimit_nofile 10000;
236+
237+
events {
238+
worker_connections 10000;
239+
multi_accept on;
240+
}
241+
```
242+
243+
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.
244+
245+
<a name="ports"></a>
246+
### Ports
247+
248+
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:
249+
250+
```sh
251+
cat /proc/sys/net/ipv4/ip_local_port_range
252+
# 32768 60999
253+
```
254+
255+
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](#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.
256+
257+
<a name="process-management"></a>
258+
### Process Management
259+
260+
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:
261+
262+
```ini
263+
[supervisord]
264+
...
265+
minfds=10000
266+
```
267+
268+
<a name="scaling"></a>
269+
### Scaling
270+
271+
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.
272+
273+
To enable horizontal scaling, you should set the `REVERB_SCALING_ENABLED` environment variable to `true` in your application's `.env` configuration file:
274+
275+
```env
276+
REVERB_SCALING_ENABLED=true
277+
```
278+
279+
Next, you should have a dedicated, central Redis server to which all of the Reverb servers will communicate. Reverb will use the [default Redis connection configured for your application](/docs/{{version}}/redis#configuration) to publish messages to all of your Reverb servers.
280+
281+
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.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.