Implementing a real-time notification system using Laravel Echo and ReactJS involves setting up a combination of Laravel Echo, a WebSocket library (such as Pusher), and ReactJS to handle the frontend updates. Below are the steps to implement this system:
Laravel Project Setup:
Ensure that your Laravel project is set up and running.
Install Laravel Echo and Pusher package using Composer:
bashcomposer require pusher/pusher-php-server
Set up your Pusher credentials in the .env
file:
envBROADCAST_DRIVER=pusher PUSHER_APP_ID=your-app-id PUSHER_APP_KEY=your-app-key PUSHER_APP_SECRET=your-app-secret PUSHER_APP_CLUSTER=your-app-cluster
ReactJS Setup:
In your Laravel project, you'll need to configure Laravel Echo to use Pusher. Open config/app.php
and add the following in the providers
array:
php'providers' => [
// ...
Laravel\Echo\EchoServiceProvider::class,
],
Then, in the same file, add the following in the aliases
array:
php'aliases' => [
// ...
'Echo' => Laravel\Echo\Facades\EchoFacade::class,
],
Create an event for your notifications. In this example, let's assume you have a NotificationEvent
:
bashphp artisan make:event NotificationEvent
Edit the generated NotificationEvent
class to include the necessary data:
phppublic $message;
public function __construct($message)
{
$this->message = $message;
}
In your notification logic, broadcast the event:
phpbroadcast(new NotificationEvent('New notification message'));
Edit the broadcasting.php
configuration file (config/broadcasting.php
). Set the driver
to pusher
and update the Pusher configuration:
php'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'encrypted' => true,
],
],
],
Install the required dependencies for ReactJS:
bashnpm install pusher-js laravel-echo react react-dom @pusher/pusher-js
Create a new React component to handle notifications:
jsx// NotificationComponent.js
import React, { useEffect } from 'react';
import Echo from 'laravel-echo';
const NotificationComponent = () => {
useEffect(() => {
const echo = new Echo({
broadcaster: 'pusher',
key: process.env.REACT_APP_PUSHER_APP_KEY,
cluster: process.env.REACT_APP_PUSHER_APP_CLUSTER,
encrypted: true,
});
echo.channel('notifications').listen('NotificationEvent', (event) => {
console.log('New notification:', event.message);
// Handle the notification as needed
});
return () => {
echo.disconnect();
};
}, []);
return <div>Notification Component</div>;
};
export default NotificationComponent;
Make sure to set the appropriate Pusher credentials in your React app environment variables.
jsx// App.js
import React from 'react';
import NotificationComponent from './NotificationComponent';
const App = () => {
return (
<div>
<h1>Your App</h1>
<NotificationComponent />
</div>
);
};
export default App;
Ensure that you import and include your React app in your Laravel Blade view.
Start your Laravel development server:
bashphp artisan serve
Start your React development server:
bashnpm start
Visit your Laravel and React applications in your web browser, and you should see real-time notifications being sent and received.
This is a basic setup, and you may need to customize it based on your specific requirements and notification types. Also, make sure to handle authentication and authorization for your real-time channels.