API Rest with Laravel 5.6 Passport Authentication — Send Notifications with Queues on Redis (Part 5)

https://medium.com/modulr/api-rest-with-laravel-5-6-175eea5db3e8

We learning how to send Notifications with management to queues using Redis. In this tutorial we will use the Notifications created previously in the previous tutorials.Image for postImage for posthttps://www.vecteezy.com

Install RedisConfig Enviroment Install Redis Driver Run the Queue Worker Install andConfigure supervisor

Step 1 Install Redis

In first step, we install Redis for this I let you two great tutorials for MacOsx and Linux.

Ubuntu 16.04

https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-redis-on-ubuntu-16-04

MacOsx

https://medium.com/@petehouston/install-and-config-redis-on-mac-os-x-via-homebrew-eb8df9a4f298Image for post

Image for post

Step 2 Config Enviroment

In this step we will config our .env file, add redis into QUEUE_DRIVER variable also change the REDIS_ variables, finally config the e-mail service. I recomend Mailtrap to test.

QUEUE_DRIVER=redisREDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=user
MAIL_PASSWORD=password
MAIL_ENCRYPTION=null

Step 3 Install Redis Driver

We need install Redis driver in to the project, using bellow command, So open your terminal or command prompt and run bellow command:

composer require predis/predis

Step 4 Run the Queue Worker

To test the queues in your local machine need run the queue worker, so run bellow command:

php artisan queue:work

You see in the terminal when the notification is processedImage for postImage for post

Step 5 Install and Configure supervisor

This step is used for production mode in your Linux server.

Install supervisor with the next command:

sudo apt-get install supervisor

Enter to folder config.d

cd /etc/supervisor/conf.d

Create config file

sudo nano laravel-worker.conf

Into the file write the next code lines

[program:laravel-worker]process_name=%(program_name)s_%(process_num)02dcommand=php /var/www/project_name/artisan queue:work redis --sleep=3 --tries=3autostart=trueautorestart=trueuser=www-datanumprocs=8redirect_stderr=truestdout_logfile=/var/www/project_name/storage/logs/worker.logstopwaitsecs=3600

Starting Supervisor

Once the configuration file has been created, you may update the Supervisor configuration and start the processes using the following commands:

sudo supervisorctl rereadsudo supervisorctl updatesudo supervisorctl start laravel-worker:*

Now we are ready to run our example so run bellow command to quick run:

php artisan serve

Tests

Now, we can simple test by rest client tools (Postman), So I test it and you can see below screenshots.

In this api you have to set two header as listed below:

Content-Type: application/json
X-Requested-With: XMLHttpRequest

Thanks for reading! I’m Alfredo Barrón, Feel free to connect with me via Twitter.

Part 1. Passport Authentication Part 2. Confirm account + notifications Part 3. Generate avatar Part 4. Reset Password Part 5. Send Notifications with Queues on Redis — Next up, Localization

Resources

References

FUll CODE

Last updated