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
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
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:

  • Installing
  • Getting started
  • Step-by-step guide
  1. Getting started
  2. Code snippets
  3. Symfony

Example of using Yandex Message Queue with Symfony

Written by
Yandex Cloud
Updated at March 28, 2025
  • Installing
  • Getting started
  • Step-by-step guide

The Symfony PHP framework contains the Messenger component allowing you to work with queues. As a message broker, you can use Message Queue.

InstallingInstalling

You will need the following tools:

  • Git
  • PHP 8.2 or higher
  • Composer package manager

Install Symfony using this guide on the official Symfony website.

Getting startedGetting started

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

Create a queue in Message Queue and copy its URL.

Step-by-step guideStep-by-step guide

In this example, you will create:

  • Demo message that stores the source numbers.
  • Message handler that sums up two numbers from the message.
  • Command that adds a task to the Message Queue queue.

To use Message Queue with Symfony Messenger, follow these steps:

  1. Create a test request named mq_example:

    symfony new --webapp mq_example
    
  2. Install dependencies to work with Amazon SQS. Message Queue uses the SQS-compatible format:

    composer require symfony/amazon-sqs-messenger async-aws/sqs ^1.9
    
  3. Create a message and a handler:

    php bin/console make:message Sum
    

    While running, the command will ask Which transport do you want to route your message to? [[no transport]].
    Enter the number that stands for the async option.

  4. Create a command for delivering messages to a queue:

    php bin/console make:command app:create
    
  5. Open the src/Command/SumCommand.php file and change it as follows:

    <?php
    
    namespace App\Command;
    
    use App\Message\Sum;
    use Symfony\Component\Console\Attribute\AsCommand;
    use Symfony\Component\Console\Command\Command;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
    use Symfony\Component\Messenger\MessageBusInterface;
    
    #[AsCommand(
        name: 'app:create',
        description: 'Add a short description for your command',
    )]
    class CreateCommand extends Command
    {
        public function __construct(private readonly MessageBusInterface $messageBus)
        {
            parent::__construct();
        }
    
        protected function execute(InputInterface $input, OutputInterface $output): int
        {
            $this->messageBus->dispatch(new Sum(4,2));
            
            return Command::SUCCESS;
        }
    }
    
  6. Open the src\Message\Sum.php file and change it as follows:

    <?php
    
    namespace App\Message;
    
    final class Sum
    {
        public function __construct(private readonly int $a, private readonly int $b)
        {
        }
    
        public function getA(): int
        {
            return $this->a;
        }
    
        public function getB(): int
        {
            return $this->b;
        }
    }
    
  7. Open the src\MessageHandler\SumHandler.php file and change it as follows:

    <?php
    
    namespace App\MessageHandler;
    
    use App\Message\Sum;
    use Symfony\Component\Messenger\Attribute\AsMessageHandler;
    
    #[AsMessageHandler]
    final class SumHandler
    {
        public function __invoke(Sum $message): void
        {
            printf('Sum of %d and %d is %d',
                $message->getA(),
                $message->getB(),
                $message->getA() + $message->getB()
            );
        }
    }
    
    
  8. Open the .env file and find the MESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=0 line in it. Edit it as follows:

    MESSENGER_TRANSPORT_DSN=sqs://message-queue.api.cloud.yandex.net/b1gvlrnlei4l********/dj6000000000********/symfony-test?access_key=KEY&secret_key=SECRET&region=ru-central1
    

    Replace b1gvlrnlei4l********/dj6000000000********/symfony-test with the path copied in the Yandex Cloud console.

    In the access_key=KEY and secret_key=SECRET parameters, replace the KEY and SECRET values with the value of the Message Queue static access key.

  9. Open the config/packages/messenger.yaml file and change it as follows:

    framework:
        messenger:
            failure_transport: failed
    
            transports:
                # https://symfony.com/doc/current/messenger.html#transport-configuration
                async:
                    dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
                    retry_strategy:
                        max_retries: 3
                        multiplier: 2
                failed: 'doctrine://default?queue_name=failed'
                # sync: 'sync://'
    
            routing:
                Symfony\Component\Mailer\Messenger\SendEmailMessage: async
                Symfony\Component\Notifier\Message\ChatMessage: async
                Symfony\Component\Notifier\Message\SmsMessage: async
                App\Message\Sum: async
    
                # Route your messages to the transports
                # 'App\Message\YourMessage': async
    
  10. Run the command for delivering messages to the queue:

    php bin/console  app:create
    
  11. Run the command for processing the queue:

    php bin/console messenger:consume async
    

Was the article helpful?

Previous
Laravel
Next
Terraform
Yandex project
© 2025 Yandex.Cloud LLC