Real-time features with Laravel Reverb

3 min read Updated 10 months ago

Real-time features with Laravel Reverb

Laravel Reverb provides WebSocket support for real-time features in your application, such as live notifications, chat messages, or status updates. Ploi Cloud makes it simple to deploy Reverb alongside your Laravel application.

What is Laravel Reverb?

Reverb is Laravel's first-party WebSocket server that enables bidirectional communication between your application and connected users. It's perfect for features that need instant updates without page refreshes.

Setting up Reverb

To add Reverb to your Laravel application on Ploi Cloud:

1. Add environment variables

In your application's Secrets tab, the environment variables below are added automatically. You generally do not have to change them.

BROADCAST_CONNECTION=reverb
REVERB_APP_ID=your-app-id
REVERB_APP_KEY=your-app-key
REVERB_APP_SECRET=your-app-secret
REVERB_HOST=your-websocket-domain
REVERB_PORT=443
REVERB_SCHEME=wss

Allowed origins

Reverb rejects WebSocket connections from origins that are not listed in REVERB_ALLOWED_ORIGINS. This variable is not set for you, so add it yourself as a comma separated list of the domains that open the connection:

REVERB_ALLOWED_ORIGINS=ploi.cloud,your-production-domain.com
  • Add your real production domain (and any staging domain) so your own frontend can connect.
  • Add ploi.cloud if you want to use the connection tester in the panel, because the browser sends ploi.cloud as the origin from there.

Leaving out ploi.cloud only breaks the tester; leaving out your own domain breaks the live connection for your users.

2. Create a Reverb worker service

Reverb runs as a dedicated worker service in your application:

  1. Navigate to your application's Services tab
  2. Click "Add service" and select "Worker"
  3. Choose "Laravel Reverb" as the worker type
  4. Enter a name like "reverb" or "websocket-server"
  5. The command is automatically set to: php artisan reverb:start --host=0.0.0.0 --port=6001
  6. Deploy your application

The Reverb service will automatically expose port 6001 for WebSocket connections.

3. Configure your Laravel application

Make sure your Laravel app has Reverb installed and configured:

composer require laravel/reverb

Update your config/broadcasting.php to use the Reverb driver, and ensure your config/reverb.php matches your environment variables.

How it works

Once deployed, your Reverb worker:

  • Runs continuously as a separate service
  • Shares the same code and storage as your main application
  • Automatically reconnects if it restarts
  • Handles all WebSocket connections on port 6001

Your main application broadcasts events to Reverb, which then pushes them to connected users in real-time.

Testing your connection

Ploi Cloud includes a built-in Reverb connection tester:

  1. Go to your application's Overview tab
  2. Find your Reverb service in the services list
  3. Click the menu (three dots) and select "Test connection"
  4. A modal will show connection details and live connection status
  5. Click "Connect" to test the WebSocket connection
  6. Watch the event log to see connection status changes

If the tester connects and immediately disconnects, ploi.cloud is missing from REVERB_ALLOWED_ORIGINS.

This helps verify that:

  • Your Reverb service is running
  • Environment variables are correct
  • WebSocket connections work properly
  • The service is accessible from outside the platform

Scaling considerations

For applications with many concurrent users:

  • Increase the Reverb worker's memory allocation in the service settings
  • Monitor the Reverb service logs for connection issues