{"id":17002261,"url":"https://github.com/ntimyeboah/laravel-queues","last_synced_at":"2026-03-06T16:03:15.165Z","repository":{"id":94684210,"uuid":"135353294","full_name":"NtimYeboah/laravel-queues","owner":"NtimYeboah","description":"Learn how to use message queues in Laravel","archived":false,"fork":false,"pushed_at":"2018-06-24T15:24:50.000Z","size":595,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-12T06:32:09.682Z","etag":null,"topics":["laravel","php","queues"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NtimYeboah.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-05-29T21:09:34.000Z","updated_at":"2021-02-13T17:52:50.000Z","dependencies_parsed_at":"2023-03-19T00:43:41.436Z","dependency_job_id":null,"html_url":"https://github.com/NtimYeboah/laravel-queues","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/NtimYeboah/laravel-queues","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NtimYeboah%2Flaravel-queues","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NtimYeboah%2Flaravel-queues/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NtimYeboah%2Flaravel-queues/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NtimYeboah%2Flaravel-queues/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NtimYeboah","download_url":"https://codeload.github.com/NtimYeboah/laravel-queues/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NtimYeboah%2Flaravel-queues/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30184885,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-06T14:42:24.748Z","status":"ssl_error","status_checked_at":"2026-03-06T14:42:14.925Z","response_time":250,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel","php","queues"],"created_at":"2024-10-14T04:27:30.046Z","updated_at":"2026-03-06T16:03:15.160Z","avatar_url":"https://github.com/NtimYeboah.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Learn how to use message queues in Laravel\n\nThis is a simple project to demonstrate how to use message queues in Laravel. In this project, we will queue emails to be sent when a user signs up with our application.\n\nIf you do not have a solid grasp of the concept of message queues, I recommend you take a look at this excellent article [here](https://daylerees.com/message-queues/) and come back.\n\nWhat happens in this application is when a user signs up, an event is emitted that pushes a notification message to a queue, then eventually the message is executed to send the mail.\n\n## Table of contents\n\n- [Installation and Setup](#installation-and-setup)\n- [Running Application](#running-application)\n- [Deep Dive](#deep-dive)\n    * [Listening to Registered event](#listening-to-registered-event)\n    * [Queuing confirmation email notification](#queuing-confirmation-email-notification)\n        + [Queuing notification](#queuing-notification)\n        + [The queue connection](#the-queue-connection)\n        + [The queue name](#the-queue-name)\n        + [Job delay](#job-delay)\n- [Factors to consider when implementing queues](#factors-to-consider-when-implementing-queues)\n    * [Log](#log)\n        + [Log job execution](#log-job-execution)\n        + [Log job failure](#log-job-failure)\n    * [Alert](#alert)\n    * [Visualize](#visualize) \n\n\n## Installation and Setup\n\nClone this repository by running\n\n```bash\n$ git clone https://github.com/NtimYeboah/laravel-queues-example.git\n```\n\nInstall the packages by running the composer command\n\n```bash\n$ composer install\n```\n\nSet your database credentials in the `.env` file\n\nSet your redis credentials in the `.env` file\n\n```bash\nREDIS_HOST=your-redis-host\nREDIS_PASSWORD=your-redis-password\nREDIS_PORT=6379\n```\n\nSign up for [Mailtrap](https://mailtrap.io/) and set your mailbox credentials in the `.env` file\n\n```bash\nMAIL_DRIVER=smtp\nMAIL_HOST=smtp.mailtrap.io\nMAIL_PORT=2525\nMAIL_USERNAME=your-mailtrap-username\nMAIL_PASSWORD=your-mailtrap-password\nMAIL_ENCRYPTION=null\n```\n\nThe confirmation email will be sent to mailtrap.\n\nRun the migrations\n\n```bash\n$ php artisan migrate\n```\n\nRun the queue\n\n```bash\n$ php artisan queue:work redis --queue=emails:verify-account\n```\n\nView queue metrics\n\nWe will use Laravel [horizon](https://laravel.com/docs/5.6/horizon) to allow us to monitor key metrics in our queue system. We can start horizon by using the artisan command:\n\n```bash\n$ php artisan horizon\n```\n\nWe can view horizon dashboard by visiting `/horizon` on your host.\n\n## Running Application\n\nRegister a new user, a message will be shown telling you to confirm your email.\n\n\n## Deep Dive\n\nWhen a user registers Laravel emits the `Illuminate\\Auth\\Events\\Registered` event. This event holds the registered user. This event is listened to and the listener pushes a confirmation email notification to the `emails:verify-account` queue.\n\n### Listening to Registered event\n\nTo listen to the `Illuminate\\Auth\\Events\\Registered` event, we register a listener in the `App\\Providers\\EventServiceProvider`. The name of the listener is `VerifyAccount`\n\n[https://github.com/NtimYeboah/laravel-queues-example/app/Providers/EventServiceProvider.php](https://github.com/NtimYeboah/laravel-queues-example/blob/master/app/Providers/EventServiceProvider.php)\n\n```php\n...\n\n/**\n * The event listener mappings for the application.\n *\n * @var array\n */\nprotected $listen = [\n    'Illuminate\\Auth\\Events\\Registered' =\u003e [\n        'App\\Listeners\\VerifyAccount',\n    ],\n];\n\n...\n```\n\nThe listener class specifies the logic to run in order to send the notification.\n\n[https://github.com/NtimYeboah/laravel-queues-example/app/Listeners/VerifyAccount.php](https://github.com/NtimYeboah/laravel-queues-example/blob/master/app/Listeners/VerifyAccount.php)\n\n```php\n...\n/**\n * Handle the event.\n *\n * @param  Registered  $event\n * @return void\n */\npublic function handle(Registered $event)\n{\n    $user = $event-\u003euser;\n\n    ...\n\n    $user-\u003esendAccountVerificationNotification($verification-\u003etoken);\n}\n...\n```\n\n### Queuing confirmation email notification\n\nThe notification to send the email will be queued so as to improve the response time of our app. Typically, you can manually test a scenario where the notification is queued and a scenario where the notification is not queued. You will notice the response is faster in the scenario where the notification is queued. You can verify this by observing the response time of each request using the network tab of chrome.\n\nTo queue a notification, we must specify the connection the notification should be sent to, the name of the queue the notification should be sent to and the time the job should wait before it's executed.\n\n#### Queuing notification\n\nTo queue notifications, you have to implement the `Illuminate\\Contracts\\Queue\\ShouldQueue` interface provided by Laravel. \n\n[https://github.com/NtimYeboah/laravel-queues-example/app/Notifications/VerifyAccountNotification.php](https://github.com/NtimYeboah/laravel-queues-example/blob/master/app/Notifications/VerifyAccountNotification.php)\n\n```php\nuse Illuminate\\Contracts\\Queue\\ShouldQueue;\n...\n\nclass VerifyAccountNotification extends Notification implements ShouldQueue\n{\n...\n\n```\n\n\n#### The queue connection\n\nWe set a property in the notification class called `connection` to the specify the connection the notification should be sent to. In our case, we will use [redis](https://laravel.com/docs/5.6/redis).\n\n```php\n...\n/**\n * The name of the connection the notification should be sent to.\n * \n * @var string|null\n */\n     \npublic $connection = 'redis';\n...\n```\n\n#### The queue name\n\nFor the queue name, we also have to set a property in the notification class to specify the queue name. In this case we call the queue name `emails:verify-account`\n\n```php\n...\n/**\n * The name of the queue the notification should be sent to.\n * \n * @var string|null\n */\npublic $queue = 'emails:verify-account';\n...\n```\n\n#### Job delay\n\nIn our case, we don't want the job to delay before it's executed. So we set the `delay` propery to `null`\n\n```php\n...\n/**\n * The time the job should wait before its executed.\n * \n * @var DateTime|null\n */\npublic $delay = null;\n...\n```\n\n### Factors to consider when implementing queues\n\nQueued jobs are executed outside of the usual request-response lifecycle, and just like any other system, things will not go according to plan. But rest assured, there are measures that can be put in place to make sure that you are in control. This section takes a look at those measures;\n\n#### Log\n\nYou can log when queue jobs are about to execute, when the job finish running and when the job fails. \n\n##### Log job execution\n\nYou can log when queue jobs are about to execute, when the job finish running. In this instance, when going through your logs and request are hitting endpoints that queue jobs however, there are no logs of jobs being executed, then you know things are wrong.\n\nLaravel provide events `before` and `after` which are fired when queues are about to be executed and when queues are done executing. You can listen to these events and log the connection, the specific job being executed and the payload or message being queued.\n\nYou can listen to these events and log in the `App\\Providers\\AppServiceProvider.php` class.\n\n[https://github.com/NtimYeboah/laravel-queues-example/app/Providers/AppServiceProvider.php](https://github.com/NtimYeboah/laravel-queues-example/blob/master/app/Providers/AppServiceProvider.php)\n\n\n```php\nuse Illuminate\\Support\\Facades\\Log;\nuse Illuminate\\Support\\Facades\\Queue;\nuse Illuminate\\Queue\\Events\\JobProcessed;\nuse Illuminate\\Queue\\Events\\JobProcessing;\n\n...\n\npublic function boot()\n{\n    Queue::before(function (JobProcessing $event) {\n        Log::info('Starting to process job', [\n              'connection' =\u003e $event-\u003econnectionName,\n              'job' =\u003e $event-\u003ejob,\n              'payload' =\u003e $event-\u003ejob-\u003epayload()\n        ]);\n    });\n\n    Queue::after(function (JobProcessed $event) {\n        Log::info('Finished processing job', [\n              'connection' =\u003e $event-\u003econnectionName,\n              'job' =\u003e $event-\u003ejob,\n              'payload' =\u003e $event-\u003ejob-\u003epayload()\n        ]);\n    });\n\n    ...\n}\n\n...\n\n```\n\n##### Log job failure\n\nYou can as well log jobs that fail to execute. You can listen to the `failing` event in the `App\\Providers\\AppServiceProvider.php` class.\n\n```php\nuse Illuminate\\Support\\Facades\\Log;\nuse Illuminate\\Support\\Facades\\Queue;\nuse Illuminate\\Queue\\Events\\JobFailed;  \n\n...\n\npublic function boot()\n{\n    Queue::failing(function (JobFailed $job) {\n        Log::error('Job failed', [\n            'connection' =\u003e $event-\u003econnectionName,\n            'job' =\u003e $event-\u003ejob,\n            'exception' =\u003e $event-\u003eexception\n        ]);\n    });\n\n    ...\n}\n\n...\n\n```\n\n\n#### Alert\n\nThere are some instance where jobs fails after reaching the maximum retry limit. In such cases, alert the user so the action can be retaken. Also you can alert you developers.\n\nIn Laravel, you can define a `failed` method that will be called when the job fails. Inside this method, you can alert the user so the action can be retaken.\n\n```php\n...\n\n/**\n * The job failed to process.\n *\n * @param  Exception  $exception\n * @return void\n */\npublic function failed(Exception $exception)\n{\n    // Send user notification of failure, etc...\n\n}\n...\n```\n\n\n#### Visualize\n\nViewing server logs is a tedious task for most developers. Again its difficult to get key metrics for you queue systems if you rely on only logs. Using a visualizing tools helps you an overview of how your queues are running. \n\nLaravel provides [horizon](https://laravel.com/docs/5.6/horizon), a beautiful dashboard and code-driven configuration for your queues when using redis as the queue driver.\n\nYou can start horizon by using the artisan command;\n\n```bash\n$ php artisan horizon\n```\n\nWe can view horizon dashboard by visiting `/horizon` on your host.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fntimyeboah%2Flaravel-queues","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fntimyeboah%2Flaravel-queues","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fntimyeboah%2Flaravel-queues/lists"}