This project implements a notification service using Laravel that allows users to subscribe to various notification types and receive notifications via email and WebSocket.
- PHP: Ensure PHP 8.2 is installed and properly configured.
- Composer: Required for managing PHP dependencies.
- Laravel: This project is built using Laravel framework version 11.9.
- Pusher PHP Server: Version 7.2 is used for handling WebSocket communication.
Clone the repository to your local machine:
git clone https://github.com/ibukunoreofe/laravel-notification-service.git
cd laravel-notification-service
Copy the example .env
file and update the environment variables with your specific configuration:
cp .env.example .env
Ensure that your database connection and mail settings are correctly configured, and add the following for WebSocket setup:
BROADCAST_CONNECTION=pusher
PUSHER_APP_ID=app-id
PUSHER_APP_KEY=app-key
PUSHER_APP_SECRET=app-secret
PUSHER_APP_CLUSTER=mt1
PUSHER_SCHEME=http
PUSHER_HOST=echo-server
PUSHER_PORT=6001
PUSHER_ENCRYPTED=false
docker-compose up --build --detach --remove-orphans
docker compose exec -it laravel.test bash
docker compose exec -it laravel.test bash
Endpoint:
POST /api/notifications/subscribe
Request Body:
{
"user_id": 1,
"notification_type": "news"
}
Description: This endpoint allows a user to subscribe to a specific notification type (e.g., news, alerts, updates).
Endpoint:
POST /api/notifications/unsubscribe
Request Body:
{
"user_id": 1,
"notification_type": "news"
}
Description: This endpoint allows a user to unsubscribe from a specific notification type.
Endpoint:
POST /api/notifications/send
Request Body:
{
"notification_type": "alert",
"title": "System Alert",
"message": "An important system alert has been triggered."
}
Description: This endpoint sends a notification to all users who are subscribed to the specified notification type.
Endpoint:
GET /api/notifications/{user_id}
Request Parameters:
user_id
: The ID of the user whose notifications you want to view.
Description: This endpoint retrieves all notifications sent to a specific user.
You can also test the WebSocket functionality using Postman. Here’s how:
-
Open Postman and select the WebSocket Request.
-
Connect to the WebSocket using the URL:
ws://localhost:6001/app/my-app-key?protocol=7&client=js&version=4.3.1&flash=false
-
Replace
my-app-key
with the key you've set in the.env
file.
Once connected, you can subscribe to a specific channel by sending the following JSON:
{
"event": "pusher:subscribe",
"data": {
"auth": "",
"channel": "notifications.alert"
}
}
After subscribing, use the Send Notification API endpoint to trigger a notification. The notification should be received in real-time in the Postman WebSocket client.
Using wscat for Testing WebSocket Connections You can use wscat, a WebSocket client for the command-line, to test your WebSocket connections.
npm install -g wscat
wscat -c ws://localhost:6001/app/app-key?protocol=7&client=js&version=4.3.1&flash=false
Replace app-key with your actual Pusher app key.
Once connected, you can subscribe to channels and listen for events.
{
"event": "pusher:subscribe",
"data": {
"auth": "",
"channel": "<channel>"
}
}
Replace my-app-key with your actual Pusher app key.
Once connected, you can subscribe to channels and listen for events.
Listen for Events:
After subscribing, any event broadcasted on the channel will be received by wscat.
Open Postman and go to the WebSocket testing feature.
ws://localhost:6001/app/my-app-key?protocol=7&client=js&version=4.3.1&flash=false
{
"event": "pusher:subscribe",
"data": {
"auth": "",
"channel": "notifications.alert"
}
}
The Project is open-sourced software licensed under the MIT license.