In any website running a queue can help to increase the runtime performance of any application by running a background queue job or request in laravel. Queues are much helpful while our application performs bulk actions like mailing to thousand of users or running a heavy tasks. In laravel we can optimize these tasks by utilizing the laravel queue services.
Laravel queue can increase the performance of the application by adding the scripts or task in order of come first, first serve. Some example of queues are sending email to user on first register or sending bulk newsletter mails.
When we use queue it sends all the task in background by putting the task in queue to process. In laravel we can configure queue in multiple ways like redis, database etc and then we can run artisan command to process the queue jobs one by one.
In this example we will send a mail in background so user won’t wait for the mail to send.
Let’s understand Run a Background Queue Job or Request in Laravel with step by step
Step 1: Create a fresh laravel project
Open a terminal window and type below command to create a new project
composer create-project --prefer-dist laravel/laravel blog
You can also read this to start with new project
Step 2: Configuration of Queue
As mentioned above laravel supports multiple drivers to support the queue so in this article i will use database driver to serve the queue so enable it in .env
file first. Change the queue driver key to database
and make database connection settings too as follow
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
QUEUE_CONNECTION=database
and mail settings also
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=youremail@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Now create queue migrations by running following command
php artisan queue:table
this will create migrations file in database migration folder as below
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
//in laravel 9
//return new class extends Migration
//and in below laravel 9
class Jobs extends Migration
{
public function up()
{
Schema::create('jobs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('queue')->index();
$table->longText('payload');
$table->unsignedTinyInteger('attempts');
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
});
}
public function down()
{
Schema::dropIfExists('jobs');
}
};
and now run artisan command to create tables of laravel queue
php artisan migrate
this will create below tables
Step 3: Create a mail class and template
In laravel 8 or 9 every mail is implements mailable class thus first we need to create a mail class as follow
php artisan make:mail QueueEmailTest
This will create a class in following location App\Mail\QueueEmailTest.php and update the below code
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class QueueEmailTest extends Mailable
{
use Queueable, SerializesModels;
private $user;
public function __construct($user)
{
$this->user=$user;
}
public function build()
{
return $this->from('from@gmail.com')
->subject('Mail from readerstacks.com')
->view('emails.test',["user"=>$this->user,"title"=>"Register"]);
}
}
and Create a simple view for mail body in resources/views/emails/test.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Readerstacks.com</title>
</head>
<body>
<h1>Hello {{ $user['name'] }}</h1>
<p>{{ $user['body'] }}</p>
<p>Simple mail using queue in background</p>
</body>
</html>
Step 4: Create a Job
Now create a job to handle our mail in background therefore create a job using following artisan command
php artisan make:job TestQueueSendEmail
Above command will create a file at location app\Jobs\TestQueueSendEmail.php
and update the code as below
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Mail\QueueEmailTest;
use Mail;
class TestQueueSendEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $data;
public function __construct($data)
{
$this->data = $data;
}
public function handle()
{
$email = new QueueEmailTest($this->data);
Mail::to($this->data['email'])->send($email);
}
}
Step 5 : Create Route and Send Mail
Now, create a route and call dispatch method to call the queue class and add this mail to queue table using inline function as below
<?php
use Illuminate\Support\Facades\Route;
use App\Jobs\TestQueueSendEmail;
Route::get("/test-queue-mail",function(){
$data=[
'email' => 'info@readerstacks.com',
"name" => "Readerstacks",
"body" => "Simple mail from background"
];
dispatch(new TestQueueSendEmail($data));
return "You have just sent a mail in background";
});
Step 6 : Run queue worker in terminal
Final step is to run the queue worker so we can process the queue in background so run below command in terminal to watch the queue
php artisan queue:listen
Above method is useful while developing the application in local server since it will automatically reload the update code without manually stop and start but in production you need to run below command in supervisor
php artisan queue:work
To run above command continuously on server you need to install supervisor https://laravel.com/docs/9.x/queues#supervisor-configuration
Screenshot: