$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[March 27, 2026] Laravel

Laravel Reverb: WebSockets

Laravel Reverb: WebSockets

────────────────────────────────────────────────────────

Laravel Reverb: WebSockets

Tip: Install Reverb

composer require laravel/reverb
php artisan reverb:install

Gotcha: Broadcast Events

class OrderShipped implements ShouldBroadcast
{
    public function broadcastOn(): Channel
    {
        return new Channel('orders');
    }
}

Tip: Listen in JavaScript

Echo.channel('orders').listen('OrderShipped', (e) => {
    console.log(e.order);
});

Gotcha: Reverb Server

php artisan reverb:start

Must be running separately from your web server.

Tip: Presence Channels

return new PresenceChannel('chat');

Track who's online in a channel.

Gotcha: Private Channels

Private channels require authentication. Users must be authorized to listen.

Tip: Use route:cache Carefully

php artisan route:cache is fast, but it doesn't work with closure-based routes. Every time you cache routes, Laravel serializes them. If you have Route::redirect() or closure callbacks, the cache breaks. Stick to controller-based routes in production.

Tip: Model APP_KEY Rotation

Rotating APP_KEY invalidates all encrypted data — cookies, encrypted DB columns, and password reset tokens. If you must rotate (e.g., after a leak), plan a migration that re-encrypts existing data with the new key.

Gotcha: Local Scope Leaks

Global scopes defined in booted() apply to ALL queries on that model — including relationships. An innocent User::all() in admin panel might exclude soft-deleted users if a global scope is active.

Senior Insight

WebSockets in Laravel have evolved significantly from the Pusher-only days, and Reverb is a massive step forward. But I've seen teams jump into real-time features without considering the operational cost. WebSocket connections consume memory on the server — each open connection is a trade-off. Plan your connection limits, implement presence channels carefully (they broadcast to ALL servers), and always have a fallback to long-polling for environments where WebSockets are blocked.

Source: Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/laravel), Spatie Blog (https://spatie.be/blog)

────────────────────────────────────────────────────────
<-- back to posts