Real-time notifications have become a crucial feature for modern web applications, enhancing user experience by delivering instant updates. Laravel provides robust tools to integrate real-time functionalities easily. In this detailed step-by-step guide, we’ll cover how to implement real-time notifications in Laravel using WebSockets, Laravel Echo, and Pusher.
Step 1: Install Laravel Echo and Pusher
Laravel Echo works seamlessly with Pusher to enable real-time capabilities.
Open your terminal and run the following command to install Laravel Echo and Pusher JavaScript libraries:
npm install --save laravel-echo pusher-js
Ensure Node.js and npm are installed on your system to execute the above command.
Step 2: Set Up a Pusher Account
Visit Pusher and create an account if you don’t already have one.
In the Pusher dashboard:
Create a new application.
Select your cluster region (e.g., mt1, ap2, etc.).
Copy the App ID, Key, Secret, and Cluster from your Pusher app.
Step 3: Configure Laravel for Broadcasting
Open your Laravel project and edit the .env file to add Pusher credentials:
Open the newly created file (app/Notifications/RealTimeNotification.php) and customize it:
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\BroadcastMessage;
class RealTimeNotification extends Notification
{
use Queueable;
private $message;
public function __construct($message)
{
$this->message = $message;
}
public function via($notifiable)
{
return ['broadcast', 'database'];
}
public function toArray($notifiable)
{
return [
'message' => $this->message,
];
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
'message' => $this->message,
]);
}
}
This notification uses the broadcast channel to send data over WebSockets and the database channel to store the notification in the database.
Step 5: Set Up a Broadcast Event
Laravel events enable broadcasting data to the frontend.
Generate an event class:
php artisan make:event NotificationSent
Open app/Events/NotificationSent.php and configure it:
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
class NotificationSent
{
use InteractsWithSockets, SerializesModels;
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function broadcastOn()
{
return new Channel('notifications');
}
}
This event broadcasts messages on the notifications channel.
Step 6: Integrate Laravel Echo on the Frontend
Open resources/js/bootstrap.js and add the following configuration:
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
forceTLS: true
});
Use Laravel Echo to listen for the NotificationSent event:
Dispatch a notification in your application. For example, from a controller:
use App\Notifications\RealTimeNotification;
$user = User::find(1); // Replace with a valid user ID
$user->notify(new RealTimeNotification('This is a test notification!'));
You should see the notification appear in real-time on the frontend if everything is configured correctly.
Step 8: Test and Debug
Test notifications in different scenarios (e.g., new message, comment, or user action).
Use the browser console and Laravel logs for debugging.
Conclusion
By following this step-by-step guide, you’ve implemented real-time notifications in Laravel using WebSockets, Pusher, and Laravel Echo. This setup ensures instant updates, making your web application more interactive and engaging for users.