laravel/reverb
Laravel Reverb adds real-time WebSocket support to Laravel applications, enabling event broadcasting and live updates via a first-party server. Designed to integrate with Laravel’s broadcasting features for fast, reliable real-time communication.
## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require laravel/reverb
php artisan reverb:install
This generates a .env configuration and publishes the necessary files.
Configuration:
Update .env with Redis connection details:
BROADCAST_CONNECTION=redis
REVERB_REDIS_URL=redis://localhost:6379
REVERB_HOST=127.0.0.1
REVERB_PORT=6001
Run Reverb:
php artisan reverb:start
For production, use a process manager like Supervisor to keep Reverb running.
First Use Case: Use Laravel's built-in broadcasting to test:
// In a route or controller
broadcast(new \App\Events\TestEvent())->toOthers();
Connect via JavaScript:
const socket = new WebSocket(`ws://${window.location.hostname}:6001/app/TEST_CHANNEL`);
socket.onmessage = (event) => console.log(event.data);
config/broadcasting.php: Configure broadcasting connections.config/reverb.php: Reverb-specific settings (e.g., host, port, heartbeat).routes/channels.php: Define channel authorization logic.app/Events/: Define your custom broadcast events.// Define an event
class MessageSent implements ShouldBroadcast
{
public function broadcastOn(): array
{
return ['message-channel'];
}
public function broadcastAs(): string
{
return 'message.sent';
}
}
// Broadcast the event
broadcast(new MessageSent($data))->toOthers();
Echo.channel('message-channel')
.listen('message.sent', (data) => {
console.log('New message:', data);
});
Use routes/channels.php to authorize channels:
Broadcast::channel('private-user.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Track users in a channel:
Broadcast::channel('chat.room.{id}', function ($user, $id) {
return $user->canAccessRoom($id);
});
// Client-side
Echo.join(`chat.room.${roomId}`)
.here((users) => console.log('Users here:', users))
.joining((user) => console.log('User joined:', user))
.leaving((user) => console.log('User left:', user));
Use Laravel's auth middleware:
Broadcast::channel('private-app.{user}', function ($user) {
return Auth::check();
});
Configure Redis pub/sub for horizontal scaling:
REVERB_SCALE=true
REVERB_SCALE_CHANNEL=reverb-scale-channel
import Echo from 'laravel-echo';
import Pusher from 'pusher-js';
window.Pusher = Pusher;
window.Echo = new Echo({
broadcaster: 'reverb',
key: 'app_key',
wsHost: window.location.hostname,
wsPort: 6001,
forceTLS: false,
disableStats: true,
enabledTransports: ['ws', 'wss'],
});
BroadcastServiceProvider and BroadcastFake for unit tests:
use Illuminate\Support\Facades\Broadcast;
public function test_broadcast_event()
{
Broadcast::fake();
broadcast(new TestEvent())->to('test-channel');
Broadcast::assertSent(TestEvent::class, function ($event) {
return $event->data === 'test';
});
}
config/reverb.php:
'log_level' => 'debug',
'log_file' => storage_path('logs/reverb.log'),
Log facade to debug connection issues:
\Log::debug('WebSocket connection established', ['connection_id' => $connection->id]);
AppServiceProvider:
public function register()
{
$this->app->bind(\Laravel\Reverb\Contracts\Connection::class, function () {
return new \App\Services\CustomReverbConnection();
});
}
6001 by default).config/reverb.php):
'allowed_origins' => ['http://localhost', 'https://your-app.com'],
redis-cli ping
storage/logs/reverb.log).REVERB_HOST and REVERB_PORT match your frontend connection.auth endpoint in Echo config.routes/channels.php.BroadcastServiceProvider includes:
Broadcast::routes(['middleware' => ['auth:sanctum']]);
php artisan reverb:auth
REVERB_ACTIVITY_TIMEOUT to prune idle connections:
REVERB_ACTIVITY_TIMEOUT=30
429 Too Many Requests errors.config/reverb.php:
'rate_limiting' => [
'enabled' => true,
'max_attempts' => 100,
'decay_seconds' => 60,
],
REVERB_SCALE_CHANNEL.REVERB_SCALE=true
REVERB_SCALE_CHANNEL=reverb-scale-channel
REVERB_SERVER_PATH=/ws
wsHost: window.location.hostname,
wsPort: 6001,
wsPath: '/ws',
REVERB_SCHEME=https
location /ws {
proxy_pass http://127.0.0.1:6001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
// config/reverb.php
'host' => env('REVERB_HOST', '127.0.0.1'),
'port' => env('REVERB_PORT', env('APP_ENV') === 'production' ? 443 :
How can I help you explore Laravel packages today?