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