Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Yandex Message Queue
    • Quick start
    • Supported tools
      • Python
      • Node.js
      • PHP
      • Celery
      • JMS
      • Laravel
      • Symfony
      • Terraform
      • Golang
  • Access management
  • Pricing policy
  • Terraform reference
  • Monitoring metrics
  • Public materials
  • FAQ

In this article:

  • Installation
  • Getting started
  • Guide
  1. Getting started
  2. Code snippets
  3. Laravel

Example of using Yandex Message Queue on Laravel

Written by
Yandex Cloud
Updated at March 28, 2025
  • Installation
  • Getting started
  • Guide

To work with queues, the Laravel PHP framework provides the Queue primitive. As a message broker, you can use Message Queue.

InstallationInstallation

Install the Laravel software using the instructions on the framework's official website.

Getting startedGetting started

  1. Create a service account.
  2. Assign the editor role to the service account.
  3. Create a static access key.

Set the environment variables:

export AWS_ACCESS_KEY_ID="<access_key_ID>"
export AWS_SECRET_ACCESS_KEY="<secret_key>"

Create a queue in Message Queue and copy the URL.

GuideGuide

In this example, we create a demo job that sums up two numbers and a command that adds the job to a queue in Message Queue.

To use Message Queue with Laravel, follow this guide.

  1. Create a test project named mq_example:

    composer create-project --prefer-dist laravel/laravel mq_example
    
  2. Create an Add job:

    php artisan make:job Add
    
  3. Open the file /app/Jobs/Add.php and reformat it as follows:

    <?php
    
    namespace App\Jobs;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Foundation\Bus\Dispatchable;
    
    class Add implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
        private $addend1 = null;
        private $addend2 = null;
    
        /**
         * Create a new job instance.
         *
         * @return void
         */
        public function __construct($addend1, $addend2)
        {
            $this->addend1 = $addend1;
            $this->addend2 = $addend2;
        }
    
        /**
         * Execute the job.
         */
        public function handle()
        {
            $result = $this->addend1 + $this->addend2;
            print("{$this->addend1} + {$this->addend2} = $result\n");
        }
    }
    
  4. Create a new ScheduleAdd command:

    php artisan make:command ScheduleAdd
    
  5. Open the file app/Console/Commands/ScheduleAdd.php and reformat it as follows:

    <?php
    
    namespace App\Console\Commands;
    
    use App\Jobs\Add;
    
    use Illuminate\Console\Command;
    
    class ScheduleAdd extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'sample:schedule-add';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Command description';
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function handle()
        {
            Add::dispatch(2, 3);
        }
    }
    
  6. Open the file config/queue.php and change the string 'default' = > env ('QUEUE_CONNECTION', 'sync'), to 'default' => 'sqs',.

  7. Fill in the parameters in the sqs section.

    To get values for the prefix and queue parameters, split the URL of your queue into two parts: the prefix is https://message-queue.api.cloud.yandex.net/, while the queue parameter is b1g8ad42m6he********/dj6000000000******** without the leading /.

    'sqs' => [
        'driver' => 'sqs',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'prefix' => env('SQS_PREFIX', 'https://message-queue.api.cloud.yandex.net/'),
        'queue' => env('SQS_QUEUE', 'b1g8ad42m6he********/dj6000000000********/laravel-test'),
        'region' => env('AWS_DEFAULT_REGION', 'ru-central1'),
    ],
    
  8. Add the aws/aws-sdk-php package to the project dependencies:

    composer require aws/aws-sdk-php
    
  9. Update the Composer configuration:

    composer update
    
  10. Run this command:

    php artisan sample:schedule-add
    
  11. Run this command:

    php artisan queue:work
    

Was the article helpful?

Previous
JMS
Next
Symfony
© 2025 Direct Cursus Technology L.L.C.