Skip to content

Configuration and Demo Project for Real-Time Notifications in Laravel (Lumen) using Laravel Echo Server, Socket.IO and Redis

License

Notifications You must be signed in to change notification settings

waqasahmedNU/laravel-live-queueing-notifications

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Real-Time/Queueing Notifications

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:

  1. 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.
  2. 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.
  3. We can use both Redis with anyother database (e.g. MySQL) for achieving both of the above requirements.

Demo API Project Setup

The demo API project is developed using Lumen Framework. To setup the project follow the steps.

  1. Clone or download the code from repository.
  2. Update .env file to connect with the database (MySQL, PostgreSQL etc.)
  3. Run command php artisan migrate to build database schema.

Setup Redis

  1. To use Redis on Windows, follow the Link.

  2. To use Redis on Linux:

    1. To install Redis on Linux sudo apt install redis-server.

    2. Use command to open this file (redis.conf) with your preferred text editor sudo nano /etc/redis/redis.conf and change supervised no to supervised systemd.

    3. Now restart the Redis service sudo systemctl restart redis.service.

    4. Test the Redis service sudo systemctl status redis.

    5. You can use Redis cli redis-cli for checking if it is working correctly.

      127.0.0.1:6379> ping
      PONG
      
    6. You can set (optional) Redis password as well. Further reading

  3. 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
    

Setup Laravel Echo Server

  1. To install Laravel Echo Server npm install -g laravel-echo-server.

  2. Configure Laravel Echo Server laravel-echo-server init.

    This will generate configuration file laravel-echo-server.json for echo server.

    {
        "authHost": "http://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 path sslCertPath and sslKeyPath. Next you need to add "https": true in subscribers. NOTE: You can modify parameters according to your need.

  3. We'll start server to check if everything is working fine laravel-echo-server start.

    NOTE: Make sure Redis is running.

  4. To test Laravel Echo Server, using the URL syntx http://{HOST}:{PORT}/apps/{APP_ID}/{method}?auth_key={KEY}.

    For example http://localhost:6001/apps/7135664adb31c6d9/status?auth_key=22b5ea0e4a06d7d299048c93daa6e11e from laravel-echo-server.json file contains,

    "clients": [
    	{
    		"appId": "7135664adb31c6d9",
    		"key": "22b5ea0e4a06d7d299048c93daa6e11e"
    	}
    ],
    

API Testing and Documentation

  1. Create Resource Notification

  2. Update Resource Notification

  3. Delete Resource Notification

About

Configuration and Demo Project for Real-Time Notifications in Laravel (Lumen) using Laravel Echo Server, Socket.IO and Redis

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages