balpom / symfony-messenger-sample
A simple example showing how to initialize and to use a Symfony Messenger with queues
Requires
- php: >=8.1
- php-di/php-di: >=7.0
- symfony/cache: >=6.4
- symfony/console: >=6.4
- symfony/doctrine-messenger: >=6.4
- symfony/event-dispatcher: >=6.4
- symfony/messenger: >=6.4
README
A simple example showing how to initialize and to use a Symfony Messenger with queues.
Not finding a simple and complete example of using the Symfony Messenger, understandable even to a dummies, I had to spend several days and create it myself. It use Doctrine with sqlite database as Message Bus transport. For Workers running uses Symfony Console. Everything was tested in Linux.
Requirements
- PHP >= 8.1
Installation
Using composer (recommended)
composer create balpom/symfony-messenger-sample
How to use
Simple test
Open console window. Run the command:
php bin/console messenger:consume doctrine-async
It starts simple Worker, which imitate SMS sending. Now it is waiting for messages to be sent from the queue, which is still empty.
Open another console window. Run the command:
php tests/send.php
It runs a simple script that adds several messages to the queue. After this, in first console window you may see, how Worker "sending" SMS.
Run the command:
php bin/console messenger:stop-workers
It stop Worker execution.
Advanced test
Open multiple consoles. In each of them, run the command:
php bin/console messenger:consume doctrine-async
It starts many simple Workers, which imitate SMS sending. Now it is waiting for messages to be sent from the queue, which is still empty.
Open one more console window. Run the command:
php tests/sendmany.php
It runs a simple script that adds many several messages to the queue. After this, in previously opened consoles you may see, how several Workers "sending" SMS.
Run the command:
php bin/console messenger:stop-workers
It stop all Workers executions.