laravel/reverb
Laravel Reverb adds real-time WebSocket communication to Laravel apps. Use it to broadcast events and power live updates with a first-party, Laravel-native server. Official docs: https://laravel.com/docs/reverb
Installation:
composer require laravel/reverb
php artisan reverb:install
This generates the Reverb configuration and publishes assets.
Configuration:
Update .env with Redis connection details:
BROADCAST_CONNECTION=redis
REVERB_REDIS_URL=redis://localhost:6379
REVERB_HOST=127.0.0.1
REVERB_PORT=8080
First Use Case: Broadcast an event to a channel:
use Illuminate\Broadcasting\Channel;
use Illuminate\Support\Facades\Broadcast;
Broadcast::channel('chat', function ($user) {
return ['id' => $user->id];
});
// In a controller or command
Broadcast::to('chat')->send(new ChatMessage('Hello!'));
Client-Side Connection:
Use the laravel-echo package with Reverb:
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'reverb',
key: 'your-app-key',
wsHost: window.location.hostname,
wsPort: 8080,
forceTLS: false,
disableStats: true,
enabledTransports: ['ws', 'wss'],
});
Run Reverb:
php artisan reverb:start
Use php artisan reverb:stop and php artisan reverb:restart as needed.
Broadcast::private('user.123')->send(new UserEvent('Updated!'));
Broadcast::channel('chat.{room_id}', function ($user, $room) {
return ['user_id' => $user->id];
});
Broadcast::to('chat.123')->send(new ChatEvent('New message'));
BroadcastServiceProvider to register event handlers:
public function boot()
{
Broadcast::route('chat', function ($user, $room) {
return ['room_id' => $room->id];
});
Broadcast::route('private-user.{id}', function ($user, $id) {
return (int) $id === $user->id;
});
}
config/broadcasting.php:
'connections' => [
'redis' => [
'driver' => 'redis',
'connection' => 'cache',
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'reverb' => [
'host' => env('REVERB_HOST', '127.0.0.1'),
'port' => env('REVERB_PORT', 8080),
],
],
],
],
Broadcast::channel('private-user.{id}', function ($user, $id) {
return (int) $id === $user->id;
});
ReverbAuthenticator or use middleware:
Broadcast::route('custom-channel', function ($user, $request) {
return $request->user()->canAccessChannel();
});
config/reverb.php:
'rate_limiting' => [
'enabled' => true,
'max_attempts' => 100,
'decay_seconds' => 60,
],
// app/Providers/ReverbServiceProvider.php
public function boot()
{
$this->app->bind(\Laravel\Reverb\Routing\Router::class, function ($app) {
return new \App\Reverb\CustomRouter($app);
});
}
laravel-echo is configured to use Reverb:
window.Echo = new Echo({
broadcaster: 'reverb',
key: 'your-app-key',
wsHost: window.location.hostname,
wsPort: 8080,
forceTLS: false,
enabledTransports: ['ws', 'wss'],
});
laravel-echo with Vue/React:
// Vue example
import Echo from 'laravel-echo';
const echo = new Echo({
broadcaster: 'reverb',
// ...config
});
echo.private(`user.${userId}`)
.listen('UserEvent', (data) => {
console.log(data);
});
ReverbTestCase for testing:
use Laravel\Reverb\Testing\ReverbTestCase;
class ChatTest extends ReverbTestCase
{
public function test_chat_broadcast()
{
$this->actingAs($user)
->assertBroadcastingOn('chat')
->broadcast(new ChatMessage('Hello!'));
}
}
[program:reverb]
command=php /path/to/artisan reverb:start
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/var/log/reverb.log
config/reverb.php:
'connection_limits' => [
'max_connections' => 5000,
'max_connections_per_ip' => 100,
],
redis-cli ping
config/reverb.php:
'allowed_origins' => [
'https://your-app.com',
'http://localhost:3000',
],
'allowed_origins' => ['*'],
php artisan reverb:stats./reverb), ensure:
'options' => [
'reverb' => [
'host' => env('REVERB_HOST', '127.0.0.1'),
'port' => env('REVERB_PORT', 8080),
'path' => env('REVERB_PATH', '/reverb'),
],
],
wsHost: window.location.hostname,
wsPort: 8080,
wsPath: '/reverb',
ShouldBroadcast and have valid JSON-serializable data:
class ChatMessage implements ShouldBroadcast
{
public function __construct(public string $message) {}
public function broadcastOn(): array
{
return ['chat'];
}
}
BroadcastServiceProvider routes are correct:
Broadcast::channel('chat.{room_id}', function ($user, $room) {
return $user->canAccessRoom($room);
});
config/reverb.php:
'log_level' => 'debug',
storage/logs/reverb.log.How can I help you explore Laravel packages today?