Laravel horizon setup and implementation

Tamrakar Shreyaa
4 min readMay 21, 2024

Laravel Horizon presents a visually appealing dashboard and enables code-driven configuration for managing Redis queues in Laravel applications. It serves as a robust management and monitoring tool, specifically crafted to streamline background job processing. By acting as a centralized hub, it provides developers with insights and control over job execution.

Real-time Monitoring: With Laravel Horizon, you get access to a visually pleasing, real-time dashboard, providing immediate insights into background job processing. This facilitates swift issue identification and proactive monitoring for optimal performance.

Task Metrics: Laravel Horizon offers detailed metrics to deepen your understanding of your application’s job queue. It provides valuable data on job execution times, success rates, and failure details, essential for fine-tuning and optimizing background tasks.

Effortless Scaling: Horizon simplifies the scaling process for Laravel applications, ensuring seamless handling of workload spikes or varying intensities without compromising performance or responsiveness.

Job Retry Management: Laravel Horizon streamlines the management of failed jobs through an intuitive interface. It allows for easy retrying of failed jobs, minimizing the impact of errors and aiding effective error resolution for a more resilient job processing system.

Intuitive Configuration: Laravel Horizon features an easy-to-configure setup, empowering developers to tailor the tool to their project’s specific requirements. Its intuitive configuration options enable customization of features to suit the unique needs of Laravel applications, ultimately enhancing development efficiency.

Install Laravel Project

Run the command to induct fresh Laravel project.

composer create-project laravel/laravel laravel-horizon

Install laravel horizon Library:

composer require laravel/horizon

php artisan horizon:install

It will install the horizon on your project. Now you can able to see the list of the horizon commands on php artisan.

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

composer require predis/predis

Setting up DB

# DB Section
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=databasename
DB_USERNAME=root
DB_PASSWORD=password

# Session section
QUEUE_CONNECTION=redis
REDIS_CLIENT=predis

# email

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_mailtrap_username
MAIL_PASSWORD=your_mailtrap_password
MAIL_ENCRYPTION=tls

Set up an email job

php artisan make:job SendEmail

This will create a new job file in the app/Jobs directory, named SendEmail.php.

<?php

namespace App\Jobs;

use App\Mail\WelcomeEmail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
use Resend\Laravel\Facades\Resend;

class SendEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $email;

/**
* Create a new job instance.
*/
public function __construct()
{

}

/**
* Execute the job.
*/
public function handle()
{
Mail::to('test@gmail.com')->send(new WelcomeEmail());
}
}
php artisan make:Mail WelcomeEmail

This will create a new mail file in the app/Mail directory, named WelcomeEmail.php.

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class WelcomeEmail extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*/
public function __construct()
{
//
}

/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Welcome Email',
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.demo',
);
}

/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}

create a new blade file in the views/emails directory, named demo.blade.php.

<h1>this is mail templete</h1>

create a new controller file in the App\Http\Controllers directory, named EmailController.php

<?php

namespace App\Http\Controllers;

use App\Jobs\SendEmail;

class EmailController extends Controller
{
public function send()
{
SendEmail::dispatch()->onQueue('emails');
return "Email queued for sending!";
}
}

Now let’s makes our required routes in routes/web.php

<?php

use App\Http\Controllers\EmailController;
use Illuminate\Support\Facades\Route;


Route::get('/demo',[EmailController::class,'send'])->name('demo');

To access the horizon

http://127.0.0.1:8000/horizon

To run horizon

php artisan horizon

#to check status
php artisan horizon:status

Output:

--

--

Tamrakar Shreyaa

Laravel | PHP | API | AJAX | jQuery | Laravel vue | Livewire | LAMP stack | CI CD