Native WebSockets with Laravel Reverb: A Hands-On Guide
Overview
Real-time features like live notifications and instant chat traditionally required third-party services like Pusher or complex manual Socket.io setups.
Prerequisites
To follow this guide, you should have a solid grasp of
Key Libraries & Tools
- Laravel Reverb: The WebSocket server engine that manages connections and message distribution.
- Laravel Echo: The JavaScript library used to subscribe to channels and listen for events on the front end.
- Artisan: Laravel's command-line interface used to boot and manage the server.
Code Walkthrough
Starting the server is the first step in enabling real-time capabilities. Open your terminal and run the following command:
php artisan reverb:start
While this gets the server running, debugging connection issues in a production-like environment requires more visibility. Use the --debug flag to see real-time message flow:
php artisan reverb:start --debug
When a user interacts with your app—for example, liking a movie—Laravel broadcasts a MovieLiked event. Reverb receives this and pushes it to all subscribed clients. You can inspect this in the browser's Network tab under the WS (WebSockets) filter. You will see frames for channel subscriptions (e.g., movies, private-user.1) and incoming event data including movie IDs and updated counts.
Syntax Notes
Reverb utilizes standard Laravel broadcasting conventions. Private channels require authentication, while presence channels allow you to track who is currently online by returning user metadata during the subscription handshake.
Practical Examples
- Live Metrics: Updating like counts or view totals across all active user sessions instantly.
- Presence Indicators: Showing a "Who's Online" list that updates as users log in or out.
- System Notifications: Pushing private alerts to specific users without requiring a page refresh.
Tips & Gotchas
Always use the debug mode during development to verify that your front end is successfully joining the correct channels. If events aren't appearing, check your .env file to ensure your broadcasting driver is set to reverb. For those who prefer a hands-off approach in production,
