Here’s a step-by-step guide to building a private chat real-time system using Laravel 11, Reverb, and Redis.
Step 1: Install Laravel 11
If you haven’t already installed Laravel 11, create a new project:
Then, install dependencies:
Step 2: Install & Configure Laravel Reverb
1️⃣ Install Reverb
2️⃣ Publish Reverb config file
3️⃣ Update .env to use Redis broadcasting
4️⃣ Update config/broadcasting.php to use Redis
5️⃣ Start Redis
Step 3: Create WebSocket Channel for Private Chat
1️⃣ Define a private channel in routes/channels.php
π‘ This ensures only authenticated users can listen to their own chat events.
Step 4: Create a Chat Model & Migration
1️⃣ Generate Chat Model & Migration
2️⃣ Define the Schema in database/migrations/YYYY_MM_DD_create_chats_table.php
3️⃣ Run Migration
Step 5: Create Chat Event
1️⃣ Generate an Event
2️⃣ Edit app/Events/MessageSent.php
π‘ This event will broadcast new messages to the receiver’s private channel.
Step 6: Create Chat Controller
1️⃣ Generate a Chat Controller
2️⃣ Edit app/Http/Controllers/ChatController.php
π‘ This method stores the message and broadcasts it to the receiver.
Step 7: Define API Route
Edit routes/api.php:
π‘ Ensure your API uses auth:sanctum or another authentication method.
Step 8: Start Reverb WebSocket Server
1️⃣ Run Laravel Queues (Redis required)
2️⃣ Start Laravel Reverb
Step 9: Frontend Integration (Example in JavaScript)
Here’s how to listen for messages using Laravel Echo:
1️⃣ Install Laravel Echo & Pusher JS
2️⃣ Configure Laravel Echo in resources/js/bootstrap.js
3️⃣ Listen for messages (Example in Vue.js or React)
Step 10: Test Everything
1️⃣ Start Laravel Backend
2️⃣ Start Reverb
3️⃣ Send a Message using Postman or Frontend
4️⃣ Check if the receiver gets the real-time message!
How to Scale Laravel Reverb for Multi-Server?
- Ensure Redis is used for broadcasting (
BROADCAST_CONNECTION=redis). - Deploy Laravel on multiple servers.
- Run
php artisan reverb:start on each server. - Use a Load Balancer (e.g., Nginx, AWS ALB) to route WebSocket traffic.
- Enable Redis clustering for large-scale chat applications.
Summary
✅ Laravel Reverb provides a native WebSocket solution for Laravel 11.
✅ Redis is used to broadcast events across multiple servers.
✅ Private Channels ensure secure messaging.
✅ Frontend listens for messages using Laravel Echo.
✅ For multi-server support, use Redis + Load Balancer.
Would you like a Docker setup for a production-ready chat app? π
XΓ‘c thα»±c Private Chat trong Laravel Reverb
Δα» ΔαΊ£m bαΊ£o chα» ngΖ°α»i dΓΉng hợp lα» mα»i cΓ³ thα» lαΊ―ng nghe vΓ nhαΊn tin nhαΊ―n trong Private Chat, Laravel sα» dα»₯ng Private Channels kαΊΏt hợp vα»i cΖ‘ chαΊΏ xΓ‘c thα»±c.
1. XΓ‘c thα»±c Private Channel
Trong routes/channels.php:
πΉ GiαΊ£i thΓch:
- Khi mα»t ngΖ°α»i dΓΉng cα» gαΊ―ng lαΊ―ng nghe kΓͺnh
chat.{receiverId}, Laravel sαΊ½ gα»i callback xΓ‘c thα»±c. - Chα» khi $user->id === $receiverId, Laravel mα»i cho phΓ©p ngΖ°α»i dΓΉng lαΊ―ng nghe kΓͺnh.
- Δiα»u nΓ y ΔαΊ£m bαΊ£o chα» ngΖ°α»i nhαΊn tin nhαΊ―n mα»i cΓ³ thα» nghe tin nhαΊ―n ΔαΊΏn.
2. XΓ‘c thα»±c NgΖ°α»i DΓΉng TrΖ°α»c Khi Gα»i Tin NhαΊ―n
Trong ChatController.php, Laravel bαΊ―t buα»c user phαΊ£i ΔΔng nhαΊp Δα» gα»i tin nhαΊ―n:
πΉ GiαΊ£i thΓch:
Auth::id() lαΊ₯y ID cα»§a user hiα»n tαΊ‘i (bαΊ―t buα»c phαΊ£i login).- ChαΊ·n gα»i tin nhαΊ―n ΔαΊΏn user khΓ΄ng tα»n tαΊ‘i vα»i
exists:users,id. - Dα»― liα»u chα» Δược lΖ°u & phΓ‘t Δi nαΊΏu user hợp lα».
3. XΓ‘c thα»±c Khi LαΊ―ng Nghe KΓͺnh TrΓͺn Frontend
Laravel Echo sαΊ½ gα»i request xΓ‘c thα»±c trΖ°α»c khi lαΊ―ng nghe Private Channel.
VΓ dα»₯, trong Vue.js/React/JavaScript:
π LΖ°u Γ½:
- Laravel sαΊ½ tα»± Δα»ng chαΊ·n kαΊΏt nα»i nαΊΏu user khΓ΄ng Δược phΓ©p nghe kΓͺnh
chat.{receiverId}. - NαΊΏu user chΖ°a ΔΔng nhαΊp, Laravel sαΊ½ tα»« chα»i kαΊΏt nα»i.
4. Laravel XΓ‘c Thα»±c Private Channel NhΖ° ThαΊΏ NΓ o?
Khi frontend yΓͺu cαΊ§u lαΊ―ng nghe mα»t Private Channel, Laravel sαΊ½:
- Kiα»m tra user ΔΓ£ ΔΔng nhαΊp hay chΖ°a.
- ChαΊ‘y callback trong
routes/channels.php Δα» xem user cΓ³ quyα»n nghe khΓ΄ng. - TrαΊ£ vα» lα»i nαΊΏu khΓ΄ng hợp lα», hoαΊ·c cho phΓ©p kαΊΏt nα»i nαΊΏu hợp lα».
π Tα»ng KαΊΏt
✅ Private Channel (chat.{receiverId}) ΔαΊ£m bαΊ£o chα» ngΖ°α»i nhαΊn mα»i Δược nghe tin nhαΊ―n.
✅ Laravel Echo gα»i yΓͺu cαΊ§u xΓ‘c thα»±c trΖ°α»c khi cho phΓ©p kαΊΏt nα»i.
✅ Backend yΓͺu cαΊ§u user phαΊ£i ΔΔng nhαΊp khi gα»i tin nhαΊ―n (Auth::id()).
✅ NαΊΏu khΓ΄ng xΓ‘c thα»±c, user khΓ΄ng thα» nhαΊn tin nhαΊ―n private.
πΉ Vα»i cΓ‘ch nΓ y, tin nhαΊ―n cα»§a user sαΊ½ an toΓ n vΓ khΓ΄ng bα» nghe lΓ©n. π