with Laravel Echo Server, Socket.IO, Redis (Queue) and MySQL (For storing notification related information)
Laravel Echo Server - NodeJs server for Laravel Echo broadcasting with Socket.io. Laravel Echo Server.
Use Laravel Echo Server and Socket.IO for broadcasting on a particular port for Real Time Notifications. Further documentation about Broadcasting and Notification.
Redis - An in-memory data structure which store data as in-memory key–value pair. Further Reading
There are several ways and reasons to show notifications:
- If you only want to show live notifcations (like the count of notifications only) we can use only Redis and set key
count
for storing the count of notifications. We can also store more values as key-value pair. - If you want to store further information about the notifcation (to show it in notificton tab) than we need to
Create table for storing notifications data
. - We can use both Redis with anyother database (e.g. MySQL) for achieving both of the above requirements.
The demo API project is developed using Lumen Framework. To setup the project follow the steps.
- Clone or download the code from repository.
- Update .env file to connect with the database (MySQL, PostgreSQL etc.)
- Run command
php artisan migrate
to build database schema.
-
To use Redis on Windows, follow the Link.
-
To use Redis on Linux:
-
To install Redis on Linux
sudo apt install redis-server
. -
Use command to open this file (redis.conf) with your preferred text editor
sudo nano /etc/redis/redis.conf
and changesupervised no
tosupervised systemd
. -
Now restart the Redis service
sudo systemctl restart redis.service
. -
Test the Redis service
sudo systemctl status redis
. -
You can use Redis cli
redis-cli
for checking if it is working correctly.127.0.0.1:6379> ping PONG
-
You can set (optional) Redis password as well. Further reading
-
-
Update the .env file in your project
BROADCAST_DRIVER=redis REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 REDIS_DB=0
-
To install Laravel Echo Server
npm install -g laravel-echo-server
. -
Configure Laravel Echo Server
laravel-echo-server init
.This will generate configuration file
laravel-echo-server.json
for echo server.{ "authHost": "https://localhost", "authEndpoint": "/broadcasting/auth", "clients": [ { "appId": "", "key": "" } ], "database": "redis", "databaseConfig": { "redis": {}, "sqlite": { "databasePath": "/database/laravel-echo-server.sqlite" } }, "devMode": true, "host": null, "port": "6001", "protocol": "http", "socketio": {}, "secureOptions": 67108864, "sslCertPath": "", "sslKeyPath": "", "sslCertChainPath": "", "sslPassphrase": "", "subscribers": { "http": true, "redis": true }, "apiOriginAllow": { "allowCors": false, "allowOrigin": "", "allowMethods": "", "allowHeaders": "" } }
To configure Laravel Echo Server for
https
, you need to update these configurations. Add the SSL Certification and Key pathsslCertPath
andsslKeyPath
. Next you need to add"https": true
insubscribers
. NOTE: You can modify parameters according to your need. -
We'll start server to check if everything is working fine
laravel-echo-server start
.NOTE: Make sure Redis is running.
-
To test Laravel Echo Server, using the URL syntx
https://{HOST}:{PORT}/apps/{APP_ID}/{method}?auth_key={KEY}
.For example
https://localhost:6001/apps/7135664adb31c6d9/status?auth_key=22b5ea0e4a06d7d299048c93daa6e11e
fromlaravel-echo-server.json
file contains,"clients": [ { "appId": "7135664adb31c6d9", "key": "22b5ea0e4a06d7d299048c93daa6e11e" } ],