Why Use Laravel Horizon and How To Set It Up?

Why Use Laravel Horizon and How To Set It Up?

Laravel Horizon is a queue manager that gives you full control over your queues. lt is a wonderful tool for executing and monitoring queue workers.

Ā·

3 min read

šŸ¤ Introducing Redis Queues

Redis Queue is a Python package that allows you to queue jobs for background processing. Because many hosting providers will time out on lengthy HTTP queries, APIs should be designed to close requests as soon as possible. We may do this using Redis Queue by routing jobs to a queue and then to a worker for processing.

With Redis queues, the processing time of queues is faster than the database queues, and to run a high number of jobs on a project, it becomes difficult to run with database queues. Also, you don't get Horizon without Redis.

šŸ¤” Why Laravel Horizon?

The Laravel horizon package is used to manage Laravel-powered Redis queues. It provides a beautiful dashboard for the queues. This package allows users to configure jobs, generate analytics, and monitor queue-related tasks, including job run-time, failure, throughput, etc. The configuration information of all the team members of the project is stored in a single file that can be controlled centrally.

This article shows you how to install and run Laravel horizon in your Laravel application. And in the end, I shall also share why I enjoy working with Laravel. Letā€™s get started.

āœ… Step 1: Installing Redis on MAC and starting the Redis server

First, let me install Redis on my system. If it is already installed in yours, start the server and jump to Step 2.

To install Redis, you need to run the following command.

brew install redis

After installing the redis, start the server by using the below command.

redis-server

If successful, you'll see the startup logs for Redis, and Redis will be running in the foreground.

āœ… Step 2: Installing predis and configurations required for Redis in our application

Coming back to our application, letā€™s add some packages. So, to use Redis with PHP, a PHP Redis client is needed. Therefore, I have installed predis in our application by using the following command.

composer require predis/predis

Open .env and set values as shown below.

Screenshot 2022-10-31 at 7.31.52 PM.png

Make changes in config/database.php and replace 'phpredis' with 'predis'.

Screenshot 2022-10-31 at 7.32.05 PM.png

āœ… Step 3: Installing Horizon package and its configuration

To install the package, run this command in the terminal

composer require laravel/horizon

After installing Horizon, publish its assets using the horizon:install Artisan command:

php artisan horizon:install

After publishing Horizon's assets, its primary configuration file will be config/horizon.php. This configuration file allows us to configure the queue worker options for our application.

āœ… Step 4: Starting the horizon

After the successful installation and configuration of the horizon. Now, letā€™s start our horizon by running the below command in the terminal.

php artisan horizon

And lastly, Horizon exposes its dashboard at the URL http://127.0.0.1/horizon.

Screenshot 2022-10-31 at 6.56.32 PM.png

ā˜‘ļø Conclusion

After following all the steps given above, you will successfully be able to integrate Laravel Horizon into your application.

As a developer, I found Horizon helpful because of the following -

  • It provides an easier way to manage Redis queues in Laravel.
  • A very readable and understandable dashboard by which we can monitor your queue system.
  • It offers various plugins to send alerts over multiple tools, such as Slack.
  • It delivers an easy-to-use code-driven configuration.
  • It also provides a very efficient auto-scaling of jobs feature.

This article helps you understand Laravel Horizon and interests you in implementing the same in your apps.

Happy Learning!!

Ā