跳至正文

WebSockets ​

Introduction ​

Laravel ships with Reverb, a local WebSocket server so you can develop realtime features with broadcasting and Laravel Echo on your machine without running an external service.

For example, while building an order tracking page, you can start Reverb alongside your application, dispatch an OrderShipped event from a handler, and watch the update arrive in your browser over a local WebSocket connection.

This server is for local development. When you deploy to production, switch your credentials to managed Reverb on Laravel Cloud. To learn how to broadcast events, authorize private and presence channels, and subscribe from the browser with Echo, check out the broadcasting guide.

The Pusher protocol ​

Reverb implements the Pusher protocol rather than a raw WebSocket wire format. Because of that, you don't need to build message framing, reconnection handling, or channel routing yourself. Laravel Echo, pusher-js, and any other Pusher-compatible client library can connect to Reverb without custom adapters, and you can move from local reverb:start to managed Reverb on Laravel Cloud by updating only your environment variables.

Rather than passing messages directly between individual sockets, the protocol organizes connections around named channels and client events:

  • Public channels let any connected client subscribe without authorization and receive broadcast events, such as deployment notices or live scores.
  • Private channels (private-*) restrict user-specific updates, such as order status changes, to authorized subscribers. In Laravel, you authorize a private channel with a single callback in routes/channels.go that returns true or false.
  • Presence channels (presence-*) build on private channels by tracking which users are currently subscribed. Reverb sends each new subscriber the current member list and notifies other subscribers as members join or leave. You authorize a presence channel in routes/channels.go by returning a *broadcast.Member with the member's details, and track who is online with Echo's useEchoPresence hook.
  • Cache channels (cache-*, private-cache-*, and presence-cache-*) remember the most recent event published to the channel and replay it immediately when a new client subscribes.
  • Client events (client-*) let subscribed browsers send messages, such as typing indicators, to other subscribers on a private or presence channel directly through Reverb without making an HTTP request to your application. Send and receive them with Echo's whisper helpers.

Reverb doesn't need access to your database or sessions. When a browser subscribes to a private or presence channel, Echo asks your application at POST /broadcasting/auth, and Laravel signs the authorization with REVERB_APP_SECRET so Reverb can verify it directly. Your application uses the same secret to sign the HTTP requests that publish broadcast events to Reverb.

Configuration ​

Reverb reads its server address and application settings from environment variables in your .env file. For most local setups, you need to set only your application credentials and can keep the defaults for everything else.

Application credentials ​

Before a browser or your application can talk to Reverb, both sides need matching application credentials to verify WebSocket connections and signed broadcast requests. Add REVERB_APP_ID, REVERB_APP_KEY, and REVERB_APP_SECRET to your .env file:

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

reverb:start reads the same three variables that the reverb broadcast connection uses, so your application and local Reverb server stay in sync without duplicating credentials. Set all three values before starting the server, or reverb:start exits with an error.

Keep REVERB_APP_SECRET on the server. Browsers need only REVERB_APP_KEY, which your .env file exposes to Vite as VITE_REVERB_APP_KEY. To learn how to enable BROADCAST_CONNECTION=reverb and configure Echo in the browser, check out the broadcasting guide.

Server address ​

By default, Reverb listens on 0.0.0.0 and port 8080 so it accepts connections on every network interface. If port 8080 is already in use on your machine or you want to bind only to loopback, set REVERB_SERVER_HOST and REVERB_SERVER_PORT in your .env file.

Keep REVERB_SERVER_HOST and REVERB_SERVER_PORT distinct from REVERB_HOST and REVERB_PORT. REVERB_SERVER_HOST and REVERB_SERVER_PORT tell reverb:start which address to bind to, while REVERB_HOST and REVERB_PORT tell your application and browsers where to connect. For example, Reverb may listen on 0.0.0.0:8080 while your application and browsers connect to localhost:8080:

ini
REVERB_SERVER_HOST=0.0.0.0
REVERB_SERVER_PORT=8080

REVERB_HOST=localhost
REVERB_PORT=8080

You may also configure two optional server settings in .env when your local setup needs them:

VariableDefaultPurpose
REVERB_SERVER_PATHEmptyPrefixes every Reverb route with a path, such as /reverb, when a reverse proxy forwards a subpath to Reverb.
REVERB_MAX_REQUEST_SIZE10000Sets the largest HTTP API request body Reverb accepts, in bytes. Larger requests receive a 413 response.

TLS ​

When your local application serves pages over HTTPS, browsers block insecure ws:// connections from those pages. You may have reverb:start serve wss:// and https:// directly by pointing REVERB_SERVER_TLS_CERT at a PEM certificate file in your .env file.

Your application's reverb broadcaster trusts your operating system's certificate store, so a certificate signed by a locally trusted certificate authority works for both browsers and your application. For example, you may install a local certificate authority and generate a localhost certificate in your application's root directory with mkcert:

shell
mkcert -install
mkcert localhost

mkcert localhost creates localhost.pem and localhost-key.pem. Point REVERB_SERVER_TLS_CERT and REVERB_SERVER_TLS_KEY at those files in .env, set REVERB_HOST to a hostname the certificate covers, and set REVERB_SCHEME to https:

ini
REVERB_SERVER_TLS_CERT=localhost.pem
REVERB_SERVER_TLS_KEY=localhost-key.pem

REVERB_HOST=localhost
REVERB_SCHEME=https

With REVERB_SCHEME=https, Echo connects over wss:// and your application publishes broadcasts over https://. When REVERB_SERVER_TLS_CERT is set, reverb:start accepts only TLS connections on its port and prints Reverb running at wss://... when it starts.

Relative paths resolve against your application's base path, and the certificate file may hold intermediate certificates after the server's certificate. You need REVERB_SERVER_TLS_KEY only when the private key is in a separate file. When REVERB_SERVER_TLS_KEY is unset, reverb:start reads the key from the certificate file. If the certificate file holds no key, reverb:start exits with an error that tells you to set REVERB_SERVER_TLS_KEY.

Allowed origins ​

By default, Reverb accepts WebSocket connections from any origin (*). If you want to mirror stricter origin rules while developing locally, set REVERB_APP_ALLOWED_ORIGINS to a comma-separated list of hostnames:

ini
REVERB_APP_ALLOWED_ORIGINS=localhost,*.myapp.test

Reverb compares each entry against the hostname of the browser's Origin header, so list hostnames rather than full URLs with schemes or ports. A * wildcard matches any sequence of characters in the hostname. For example, *.myapp.test allows http://app.myapp.test:5173, and it rejects both https://myapp.test and connections without an Origin header.

Connection timeouts and limits ​

Reverb checks that idle connections are still alive and caps message sizes and subscriptions for each application. The defaults work for most local development, and you may override any of them in .env:

VariableDefaultPurpose
REVERB_APP_PING_INTERVAL60Seconds a connection may stay silent before Reverb pings it. If the connection stays silent for that long again, Reverb closes it.
REVERB_APP_ACTIVITY_TIMEOUT30Seconds browsers wait without hearing from Reverb before sending a ping.
REVERB_APP_MAX_MESSAGE_SIZE10000Largest WebSocket message a browser may send, in bytes.
REVERB_APP_MAX_CONNECTIONSNo limitMaximum simultaneous open WebSocket connections for the application.
REVERB_APP_MAX_CHANNELS_PER_CONNECTION1000Maximum channels a single browser connection may subscribe to at once.

Client events ​

When browsers send client events such as typing indicators with Echo's whisper method, Reverb relays those client- events directly to the other subscribers on the channel. The REVERB_APP_ACCEPT_CLIENT_EVENTS_FROM variable controls which connections may send them:

  • members (default): Allows only connections subscribed to the channel to send client events. On presence channels, Reverb attaches the sender's presence user_id so a browser cannot impersonate another member.
  • all: Allows any open connection to send client events, even if it is not subscribed to the channel.
  • none: Disables client events and replies to each one with an error.

For example, to turn off client events for your local application, set the variable to none in .env:

ini
REVERB_APP_ACCEPT_CLIENT_EVENTS_FROM=none

Rate limiting ​

Reverb can cap how many messages each browser connection may send in a time window. Rate limiting is off by default, and you may turn it on in .env when testing how your frontend handles throttled connections:

ini
REVERB_APP_RATE_LIMITING_ENABLED=true
REVERB_APP_RATE_LIMIT_MAX_ATTEMPTS=60
REVERB_APP_RATE_LIMIT_DECAY_SECONDS=60
REVERB_APP_RATE_LIMIT_TERMINATE=false

Each window starts when a connection sends its first message and lasts for REVERB_APP_RATE_LIMIT_DECAY_SECONDS seconds (default 60). Within that window, a connection may send up to REVERB_APP_RATE_LIMIT_MAX_ATTEMPTS messages (default 60). When a connection exceeds the limit, Reverb replies with a rate-limit error and leaves the connection open by default. Set REVERB_APP_RATE_LIMIT_TERMINATE=true to close the connection instead.

Serving multiple applications ​

Environment variables configure one application, and that covers most projects. When you develop two applications side by side on your machine, such as a storefront and an admin dashboard, one local Reverb server can serve both if you register the second application in your Configure function in bootstrap/app.go, as the configuration guide describes.

By the time configure runs, cfg.Reverb already holds your primary application from .env. Build each extra config.ReverbApp with config.NewReverbApp, adjust any fields you need, and append it to cfg.Reverb.Apps:

go
// bootstrap/app.go
package bootstrap

import (
	laravel "go.laravel.com"
	"go.laravel.com/config"
)

func Options() laravel.Options {
	return laravel.Options{
		Configure: configure,
		// ...
	}
}

func configure(cfg *config.Config, env *config.Env) {
	admin := config.NewReverbApp(
		env.Require("ADMIN_REVERB_APP_ID"),
		env.Require("ADMIN_REVERB_APP_KEY"),
		env.Require("ADMIN_REVERB_APP_SECRET"),
	)
	admin.AllowedOrigins = []string{"admin.myapp.test"}
	admin.ClientEvents = config.ClientEventsFromNone

	cfg.Reverb.Apps = append(cfg.Reverb.Apps, admin)
}

Running the server ​

Once you set your credentials in .env, start the local Reverb server using the reverb:start command:

shell
laravel reverb:start

Reverb listens on 0.0.0.0:8080 by default and prints its WebSocket URL when it is ready. Press Ctrl+C to stop the server. Reverb closes open connections before exiting.

If you need a different host or port for a single run without changing .env, pass --host or --port:

shell
laravel reverb:start --host=127.0.0.1 --port=9000

If your setup serves Reverb under a path prefix, pass --path to override REVERB_SERVER_PATH for that run:

shell
laravel reverb:start --path=/reverb

Because Laravel delivers broadcast events through your queue by default, run your web server and queue worker in another terminal alongside laravel reverb:start. Running laravel dev starts the web server, queue worker, and Vite together.

Debugging ​

By default, Reverb prints only its startup address. When you troubleshoot a missed event or a failed subscription, pass --debug to watch live server activity:

shell
laravel reverb:start --debug

With --debug enabled, Reverb prints connections opening and closing, channel subscriptions and unsubscriptions, whispers, published events with their recipient counts, and protocol errors.

Production ​

The reverb:start server is for local development. It runs in a single process, keeps all connections and channel state in memory, and does not scale across multiple servers.

In production, use managed Reverb on Laravel Cloud, which handles TLS and scaling for you. Point your production REVERB_APP_ID, REVERB_APP_KEY, REVERB_APP_SECRET, REVERB_HOST, REVERB_PORT, and REVERB_SCHEME variables at your Laravel Cloud Reverb cluster, and your application will publish to it without running reverb:start.