This is a sample project for implementing realtime feature for laravel using these packages:
- Laravel websockets (https://github.com/beyondcode/laravel-websockets)
- Laravel Echo (https://github.com/laravel/echo)
- Noty JS (https://ned.im/noty/v2)
This project is just an example impelemntation, so we will have:
- A basic job run for 5 seconds in the background, then notify all users after done (without refreshing the browser).
- A basic job run for 5 seconds in the background, then notify only the current user (privately, also without refreshing the browser).
- PHP ^7.2 (And meet the laravel 7.x server requirements).
- MySQL or MariaDB or Sqlite database.
- Clone the repo:
$ git clone https://github.com/nafiesl/laravel-queue-notifier-example.git
$ cd laravel-queue-notifier-example
$ composer install
$ cp .env.example .env
$ php artisan key:generate
- Create a MySQL/MariaDB/Sqlite database for this project
- Set the database credential on
.env
file $ php artisan migrate
$ php artisan serve
- Open a new terminal tab
$ php artisan queue:work
- Open a new terminal tab (again)
$ php artisan websockets:serve
- The project is ready to use.
- Open the web page via browser
http://127.0.0.1:8000
(the given link on How to Install step #9) - Register as a new user:
John
(we will be redirected to the/home
route) - Open a new browser with incognito/private mode, go to
http://127.0.0.1:8000
- Register as a new different user:
David
(we will be redirected to the/home
route as well) - When John hits the Run a long job button
- John will be redireted back to home
- John will get a notifier message on the bottom right
Please wait, your request is processing...
. - Don't refresh the page, just wait...
- After 5 seconds, both John and David will get a notifier message
Long run job done after 5 seconds
- John and David get the notifier, because this is a public channel for all users.
- When David hits the Run a long job button, the same behavior on point #5 happens for David.
- When David hits the Run a long private job button
- David will be redirected back to home
- Davil will get a notifier message on the bottom right
Please wait, your request is processing...
. - Don't refresh the page, just wait...
- After 5 seconds, both David will get a notifier message
Long run private job (for '...') done after 5 seconds
- John will not get the notifier, because this is a private channel for David.
- When John hits the Run a long private job button, the same behavior on point #7 happens for John.
This sample project is a free and open-source under MIT license.