This Laravel application now includes a real-time chat feature that allows authenticated users to communicate instantly using WebSockets.
- Real-time message delivery using Laravel Broadcasting
- WebSocket support via Pusher/Laravel Echo
- Clean, responsive chat interface built with Livewire
- Message history (last 50 messages)
- User identification for each message
- Timestamps for all messages
The necessary packages are already included in composer.json and package.json:
- Laravel Echo (JavaScript)
- Pusher JS (JavaScript)
- Laravel Reverb is included for local WebSocket server
Update your .env file with the appropriate broadcast driver:
BROADCAST_DRIVER=pusher
# For Pusher
PUSHER_APP_ID=your-app-id
PUSHER_APP_KEY=your-app-key
PUSHER_APP_SECRET=your-app-secret
PUSHER_APP_CLUSTER=mt1
# Vite variables
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"For local development with Laravel Reverb (recommended):
BROADCAST_DRIVER=reverb
REVERB_APP_ID=your-app-id
REVERB_APP_KEY=your-app-key
REVERB_APP_SECRET=your-app-secret
REVERB_HOST="localhost"
REVERB_PORT=8080
REVERB_SCHEME=httpphp artisan migrateThis will create the chat_messages table.
No additional server needed. Just configure your Pusher credentials.
php artisan reverb:startnpm install
npm run buildFor development with hot reload:
npm run devphp artisan serve- Access the Chat: Navigate to
/chatroute (requires authentication) - Send Messages: Type your message in the input field and click "Send" or press Enter
- Real-time Updates: Messages from other users will appear automatically without page refresh
- Message History: The chat loads the last 50 messages on page load
The chat_messages table includes:
id: Primary keyuser_id: Foreign key to users tablemessage: Text field for the message contentcreated_atandupdated_at: Timestamps
- Channel:
chat(public channel) - Event:
MessageSent - Payload includes: message ID, user ID, user name, message content, and timestamp
- Models:
app/Models/ChatMessage.php - Events:
app/Events/MessageSent.php - Livewire:
app/Livewire/Chat.php - Views:
resources/views/livewire/chat.blade.php,resources/views/chat.blade.php - Routes: Updated
routes/web.phpandroutes/channels.php - Migrations:
database/migrations/2026_02_14_124607_create_chat_messages_table.php - JavaScript:
resources/js/bootstrap.js - Tests:
tests/Feature/ChatTest.php
- All chat routes require authentication
- Messages are associated with authenticated users
- Input validation prevents messages over 500 characters
- XSS protection through Blade's automatic escaping
In app/Livewire/Chat.php, modify the loadMessages() method:
->take(50) // Change this numberThe chat interface uses Tailwind CSS. Modify resources/views/livewire/chat.blade.php to customize the appearance.
To implement private chat rooms, modify the broadcasting channel in app/Events/MessageSent.php:
return [
new PrivateChannel('chat.' . $roomId),
];Run the chat feature tests:
php artisan test tests/Feature/ChatTest.phpOr with Pest:
vendor/bin/pest tests/Feature/ChatTest.php- Verify the WebSocket server is running
- Check browser console for connection errors
- Ensure
.envvariables are set correctly - Clear Laravel cache:
php artisan config:clear
- Clear node_modules:
rm -rf node_modules && npm install - Clear build cache:
npm run build -- --force
- Run migrations:
php artisan migrate - Check database connection in
.env