Laravel Reverb is a built-in WebSocket server introduced in Laravel 11 that allows you to create real-time applications without needing third-party services like Pusher or Laravel WebSockets.
π₯ In short: Laravel Reverb makes it easy to implement WebSockets in Laravel without any extra setup or costs.
π Why use Laravel Reverb?
✅ No need for Pusher → Reduces costs, no third-party dependencies.
✅ Built into Laravel 11 → No need to install Laravel WebSockets.
✅ High performance → Optimized for Laravel applications.
✅ Easy to configure & deploy → Runs as a service within Laravel.
π§ How to Use Laravel Reverb?
1️⃣ Enable Reverb in Laravel
Open your .env file and set the broadcast driver to reverb:
Start the Reverb WebSocket server with:
By default, Reverb runs on port 6001.
2️⃣ Create an Event for Broadcasting Messages
π Generate a new event:
π Edit app/Events/MessageSent.php:
π Explanation:
- This event broadcasts on the
chatchannel. - When a message is sent, all clients listening to this channel will receive the event in real-time.
3️⃣ Update the Controller to Send Messages
π Create ChatController:
π Modify app/Http/Controllers/ChatController.php:
π Explanation:
- When a user sends a message, it broadcasts (
broadcast()) theMessageSentevent over WebSockets.
4️⃣ Add API Routes for Chat
π Open routes/api.php and add:
π Now, the API will send messages in real-time!
5️⃣ Set Up Laravel Echo on Frontend
π Install Laravel Echo & Reverb Client:
π Open resources/js/bootstrap.js and update:
π Explanation:
window.Echo.channel('chat')→ Listens for theMessageSentevent on thechatchannel.- When a new message is received, it will log the message to the console.
π Rebuild the frontend assets:
6️⃣ Start WebSocket and Test Chat
π Run Laravel server:
π Start Laravel Reverb WebSocket:
π Send a message via API:
π If successful, the frontend console should display the message in real-time! π
π― Summary
✅ Laravel Reverb allows you to implement WebSockets without needing Pusher.
✅ Simple setup—just start with php artisan reverb:start.
✅ Works seamlessly with Laravel Echo for real-time events.
✅ Integrates easily with Vue.js, React, or Blade templates.
π Now you have a real-time chat system in Laravel 11 without using any third-party services! π
Integrating Laravel Reverb with Vue 3 + TypeScript for Real-time Chat
Since you're using Vue 3 with TypeScript, you'll need to set up Laravel Echo with Reverb in Vue properly.
π 1. Install Dependencies
In your Vue 3 project, install Laravel Echo and Reverb client:
π 2. Set Up Laravel Echo in Vue 3 (TypeScript)
Create a new file src/utils/reverb.ts to handle WebSocket connections:
π 3. Implement Chat Component in Vue 3
Create a new component src/components/Chat.vue:
π‘ 4. Update Laravel Backend
Make sure you have these APIs in Laravel 11:
π Modify routes/api.php:
π Modify app/Http/Controllers/ChatController.php:
π Modify app/Events/MessageSent.php:
π₯ 5. Start the Application
Run Laravel server:
Start Laravel Reverb WebSocket:
Run Vue frontend:
π― Summary
✅ Laravel 11 Backend
- Uses Laravel Reverb instead of Pusher.
- API for sending & fetching messages.
- Broadcasts messages using Laravel Echo.
✅ Vue 3 + TypeScript Frontend
- Uses Laravel Echo with Reverb for real-time messaging.
- Listens for events and updates the chat UI.
- API calls for sending & receiving messages.
π Now you have a real-time chat system with Laravel Reverb + Vue 3 + TypeScript! π
Thank you
No comments:
Post a Comment