DEV Community

Cover image for Redis Pub/Sub vs Laravel Reverb: Real-Time Laravel at Its Best
Alex Beygi
Alex Beygi

Posted on

Redis Pub/Sub vs Laravel Reverb: Real-Time Laravel at Its Best

Both Redis Pub/Sub and Laravel Reverb can be used for real-time functionality in Laravel applications, but they address different layers of the real-time stack and have distinct benefits and use cases:

Redis Pub/Sub is a messaging paradigm where publishers send messages to channels, and subscribers listen to those channels. It's fast, lightweight, and widely used for real-time messaging.

Laravel Reverb is Laravel's first-party WebSocket server introduced in Laravel 11, aiming to offer a robust real-time solution tightly integrated with Laravelโ€™s broadcasting system.

Feature Redis Pub/Sub Laravel Reverb
Speed Extremely fast (in-memory) Fast with persistent WebSocket connections
Latency Sub-millisecond Sub-millisecond (depending on network/WebSocket load)
Horizontal Scaling Tricky (no persistence) Cluster support, queue integration
Message Durability None (ephemeral) Optional via queues and Redis
Connection Type Stateless Persistent (WebSocket)

Performance

Redis Pub/Sub
Speed: Extremely fast due to being in-memory and low-overhead.

Latency: Sub-millisecond for local setups.

Horizontal Scaling: Pub/Sub doesn't support message persistence or queuing, which can be a limitation in distributed systems.

Load Handling: Handles high-throughput messaging, but can drop messages if subscribers disconnect.

Redis::publish('channel-name', json_encode(['message' => 'Hello']));

On the subscriber side, you'd need a separate process (often in Node.js or Go) to receive messages and forward to clients.

Laravel Reverb
Speed: Built on top of FrankenPHP or Swoole, it's designed for high concurrency and persistent WebSocket connections.

Latency: Excellent for WebSocket-based communication; real-time delivery with persistent connections.

Horizontal Scaling: Supports clustering and integrates tightly with Laravel queues and Redis.

Reliability: Built-in reconnection and session handling. More resilient than raw Pub/Sub.

Example:

Broadcasting Event:

event(new MessageSent($message));

Client Side (with Laravel Echo):

Echo.channel('chat')
    .listen('MessageSent', (e) => {
        console.log(e.message);
    });
Enter fullscreen mode Exit fullscreen mode

Verdict:
Redis Pub/Sub is ideal for backend-to-backend communication or ephemeral events.

Reverb shines in persistent client communication and scales more gracefully in Laravel-native projects.

Conclusion
If you're looking to build real-time features in Laravel, Laravel Reverb is the clear winner in terms of usability, integration, and developer experience. Itโ€™s made for Laravel, integrates with everything you already use, and takes away the pain of setting up a real-time infrastructure.

That said, Redis Pub/Sub is still a great choice for lightweight, internal messaging systems or cross-service communication where WebSockets aren't needed.

Top comments (0)