In last blog Adding Notifications and Toast Messages in Livewire 3.0, we explored how to add notifications and toast messages in Livewire 3.0. Today, we’ll dive into building a real-time chat application using Livewire 3.0. Real-time chat apps are integral to modern web applications, enabling seamless communication between users. With Livewire’s reactivity, creating such a feature becomes simple and efficient.
Generate a new Livewire component for the chat feature:
php artisan make:livewire Chat
This creates:
app/Livewire/Chat.php
– The backend logic for the chat.
resources/views/livewire/chat.blade.php
– The frontend view for the chat interface.
Open Chat.php
and define properties and methods:
'fetchMessages'];
public function mount()
{
$this->fetchMessages();
}
public function fetchMessages()
{
$this->messages = Message::latest()->take(20)->get()->reverse();
}
public function sendMessage()
{
$this->validate([
'newMessage' => 'required|max:255',
]);
Message::create([
'user_id' => Auth::id(),
'content' => $this->newMessage,
]);
$this->newMessage = '';
$this->emit('messageReceived');
}
public function render()
{
return view('livewire.chat');
}
}
fetchMessages
: Fetches the latest 20 messages from the database.
sendMessage
: Validates and saves new messages to the database.
$listeners
: Listens for the messageReceived
event to update messages in real time.
Open chat.blade.php
and build the chat layout:
@foreach ($messages as $message)
{{ $message->user->name }}: {{ $message->content }}
@endforeach
Run the following command:
php artisan make:migration create_messages_table
In the migration file, define the schema:
Schema::create('messages', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->text('content');
$table->timestamps();
});
Run the migration:
php artisan migrate
php artisan make:model Message
Update Message.php
:
belongsTo(User::class);
}
}
For real-time broadcasting, you can use Laravel Echo and Pusher:
Install Laravel Echo and Pusher:
npm install --save laravel-echo pusher-js
Configure broadcasting in .env
:
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=your-app-id
PUSHER_APP_KEY=your-app-key
PUSHER_APP_SECRET=your-app-secret
PUSHER_APP_CLUSTER=your-cluster
Update the sendMessage
method to broadcast events:
event(new MessageSent($message));
Listen for the broadcast event in your JavaScript:
Echo.channel('chat')
.listen('MessageSent', (e) => {
Livewire.emit('messageReceived');
});
By following these steps, your chat application will:
Allow users to send and receive messages in real time.
Dynamically update the message list without refreshing the page.
Provide an interactive and user-friendly experience.
Creating a real-time chat application with Livewire 3.0.
Managing messages dynamically with Livewire properties.
Setting up the database for chat functionality.
(Optional) Integrating real-time broadcasting with Laravel Echo.
410 C, Jaina Tower-1, District Center, Janak Puri New Delhi-110058, India.
© Copyright 2025 Wontonee. All Right Reserved.