Real-Time Laravel with Reverb
Real-Time Laravel with Reverb
Building real-time features in Laravel applications just got significantly easier. Laravel Reverb is a first-party WebSocket server that enables Laravel real-time communication between your server and clients without relying on external services. Whether you’re building live notifications, chat systems, or collaborative tools, Reverb provides a seamless solution for real-time Laravel applications.
What is Laravel Reverb?
Laravel Reverb is one of the newest additions to the Laravel ecosystem. This first-party package allows your application to communicate in real-time between client and server using WebSockets. Before Reverb, implementing Laravel real-time features typically meant integrating third-party services like Pusher.
With Laravel Reverb, you can now build real-time Laravel applications that seamlessly integrate with other parts of the Laravel ecosystem like Laravel Echo for client-side listening and Laravel Forge for easy deployment.
Why Use Laravel Reverb for Real-Time Applications?
- No Third-Party Dependencies: Host your own WebSocket server without monthly subscription costs or external service limitations
- Native Laravel Integration: Works seamlessly with Laravel Broadcasting and Events systems you already know
- Easy Deployment: First-class support in Laravel Forge makes production deployment straightforward
- Cost-Effective: Eliminate per-message costs associated with services like Pusher
- Full Control: Complete ownership of your real-time infrastructure and data
Common Use Cases for Laravel Real-Time Applications
- Live Notifications: Push instant updates to users without page refreshes
- Chat Applications: Build messaging systems with real-time message delivery
- Collaborative Tools: Enable multiple users to work together with live updates
- Dashboard Analytics: Display real-time metrics and data visualisations
- Progress Tracking: Show live progress bars for long-running operations
- Live Activity Feeds: Update social feeds or activity streams instantly
How Laravel Real-Time with Reverb Works
Getting started with Laravel real-time using Reverb is straightforward. You can install broadcasting support using the php artisan install:broadcasting command, which will prompt you to install Laravel Reverb. Once installed, Reverb credentials are generated in your .env file and a configuration file is created at config/reverb.php.
Setting Up Broadcasting
With Laravel Broadcasting integrated, you can use Laravel Events to broadcast messages onto channels in real-time. Laravel real-time with Reverb supports both public and private channels. Public channels are available to all connected clients, while private channels can be scoped to specific authenticated users.
Broadcasting on Public Channels
To broadcast on a public channel for your Laravel real-time application, define a channel using the broadcastOn method in your event:
use IlluminateBroadcastingChannel;
public function broadcastOn(): Channel
{
return new Channel('my-public-channel');
} Broadcasting on Private Channels
For user-specific real-time updates in Laravel, use a private channel:
use IlluminateBroadcastingChannel;
use IlluminateBroadcastingPrivateChannel;
public function broadcastOn(): Channel
{
return new PrivateChannel('users.'.$this->user->id.'.notifications');
} Listening for Real-Time Events with Laravel Echo
On the client side, Laravel Echo listens to the WebSocket events broadcast from your Laravel real-time server. First, define the channel you’re listening to, then specify which event to listen for using the event class name:
window.Echo.channel('my-public-channel')
.listen('PublicNotificationEvent', (e) => {
// Handle the real-time event here
console.log('Received real-time update:', e);
}); Any public properties defined in your event class will be available through the callback parameter, making it easy to pass real-time data to your frontend.
Listening on Private Channels
For private channels in your Laravel real-time application, use the private method:
window.Echo.private(`users.${userId}.notifications`)
.listen('UserNotificationEvent', (e) => {
// Handle private real-time notification
}); Activating Laravel Reverb in Forge
Deploying Laravel real-time applications with Reverb on Laravel Forge is remarkably simple. Navigate to your site from the Sites dropdown, select Application in the left sidebar, and under the Laravel section you’ll find a toggle to activate Laravel Reverb. Enable it, ensure your queue is configured correctly, and your Laravel real-time features with Reverb will be live.
For production deployments, make sure to review the Laravel Reverb production documentation for best practices on scaling and securing your real-time Laravel infrastructure.
FAQ: Laravel Real-Time with Reverb
Q: Do I need to keep using Pusher if I switch to Laravel Reverb? A: No, Laravel Reverb is a complete replacement for Pusher and other WebSocket services. You can migrate entirely to Reverb for your Laravel real-time needs.
Q: Can I use Laravel Reverb with existing Laravel Echo code? A: Yes, Laravel Echo works seamlessly with Reverb. You’ll just need to update your Echo configuration to point to your Reverb server instead of Pusher.
Q: Is Laravel real-time with Reverb production-ready? A: Yes, Laravel Reverb is production-ready and officially supported by the Laravel team with built-in deployment support for various Laravel services like Forge or Cloud.
Q: What are the server requirements for Laravel Reverb? A: Reverb requires PHP 8.2+ and runs as a long-lived process. It’s recommended to use a process manager like Supervisor to keep it running. I have seen some mentions of servers running Reverb running into issues with high CPU usage, many have been reported in the Github issues section of the repository, for example.
Conclusion
Laravel real-time with Reverb represents a significant step forward for the Laravel ecosystem. By providing a first-party WebSocket solution, Laravel makes it easier than ever to build real-time features without external dependencies or complex infrastructure. Whether you’re building notifications, chat systems, or collaborative tools, Reverb offers a cost-effective and developer-friendly approach to real-time Laravel applications.