Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Daniyal Javani
Daniyal Javani

Posted on

     

How to Implement a Simple Queue in Symfony

Symfony is a popular PHP framework that provides many features and components to create web applications. One of these components is the Messenger component, which allows you to send and receive messages between different parts of your application or between different applications. In this article, we will see how to use the Messenger component to implement a simple queue system using Redis as the transport layer.

Installing the Required Packages

The first step is to install the Messenger component and the Redis adapter for it. You can do this by running the following commands in your project directory:

composer require symfony/messengercomposer require symfony/redis-messenger
Enter fullscreen modeExit fullscreen mode

This will add the necessary dependencies to your composer.json file and install them.

Configuring the Transport

The next step is to configure the transport that will be used to send and receive messages. In this case, we will use Redis, which is a fast and reliable in-memory data store that can act as a message broker. To use Redis, you need to have a Redis server running and accessible from your application. You can use the official Redis Docker image for this purpose.

To configure the Redis transport, you need to edit the .env file in your project root and uncomment the line that defines the MESSENGER_TRANSPORT_DSN environment variable. This variable holds the connection string to the Redis server and the name of the queue that will be used. For example, if your Redis server is running on localhost on port 6379 and you want to use a queue named messages, you can set the variable as follows:

MESSENGER_TRANSPORT_DSN=redis://localhost:6379/messages
Enter fullscreen modeExit fullscreen mode

You can also use a different queue name or a different Redis server if you want.

Creating the Message and the Handler

The next step is to create the message and the handler classes that will be used by the Messenger component. A message is a simple PHP object that represents the data that will be sent or received by the transport. A handler is a PHP class that implements the logic that will be executed when a message is received. You can create these classes using the make:message command provided by the Symfony console:

php bin/console make:message
Enter fullscreen modeExit fullscreen mode

This command will ask you to enter the name of the message class and the name of the handler class. For this example, we will use SmsNotification as the message class and SmsNotificationHandler as the handler class. The command will generate two files in the src/Message and src/MessageHandler directories respectively. You can edit these files to add the properties and methods that you need for your message and handler.

For example, the SmsNotification class could have a property that holds the phone number and a property that holds the text of the SMS:

<?phpnamespaceApp\Message;classSmsNotification{private$phoneNumber;private$text;publicfunction__construct(string$phoneNumber,string$text){$this->phoneNumber=$phoneNumber;$this->text=$text;}publicfunctiongetPhoneNumber():string{return$this->phoneNumber;}publicfunctiongetText():string{return$this->text;}}
Enter fullscreen modeExit fullscreen mode

The SmsNotificationHandler class could have a method that uses a third-party service to send the SMS:

<?phpnamespaceApp\MessageHandler;useApp\Message\SmsNotification;useSymfony\Component\Messenger\Handler\MessageHandlerInterface;classSmsNotificationHandlerimplementsMessageHandlerInterface{publicfunction__invoke(SmsNotification$smsNotification){// Use a third-party service to send the SMS// For example, using Twilio:// $twilio = new \Twilio\Rest\Client($sid, $token);// $twilio->messages->create($smsNotification->getPhoneNumber(), [//     'from' => $from,//     'body' => $smsNotification->getText(),// ]);}}
Enter fullscreen modeExit fullscreen mode

Routing the Message to the Transport

The next step is to tell the Messenger component which transport to use for each message. You can do this by editing the config/packages/messenger.yaml file and adding a routing section. In this section, you can map each message class to a transport name. For example, to use the Redis transport that we configured earlier for the SmsNotification message, you can add the following lines:

# config/packages/messenger.yamlframework:messenger:transports:async:"%env(MESSENGER_TRANSPORT_DSN)%"routing:# async is whatever name you gave your transport above'App\Message\SmsNotification':async
Enter fullscreen modeExit fullscreen mode

You can also use different transports for different messages if you want.

Dispatching the Message

The next step is to dispatch the message to the transport. You can do this by using the MessageBusInterface service that is provided by the Messenger component. You can inject this service into any class that needs to send messages, such as a controller, a service, or a command. For example, in a controller, you can dispatch a message like this:

<?phpnamespaceApp\Controller;useApp\Message\SmsNotification;useSymfony\Bundle\FrameworkBundle\Controller\AbstractController;useSymfony\Component\Messenger\MessageBusInterface;useSymfony\Component\Routing\Annotation\Route;classHomeControllerextendsAbstractController{#[Route('/home', name: 'app_home')]publicfunctionindex(MessageBusInterface$messageBus):JsonResponse{$messageBus->dispatch(newSmsNotification('1234567890','Hello from Symfony!'));return$this->json(['message'=>'Message has been dispatched successfully!',]);}}
Enter fullscreen modeExit fullscreen mode

This will send the message to the Redis transport, where it will be stored until it is consumed by a worker.

Consuming the Message

The final step is to consume the message from the transport and execute the handler. You can do this by using the messenger:consume command provided by the Symfony console:

php bin/console messenger:consume async-vv
Enter fullscreen modeExit fullscreen mode

This command will start a worker process that will listen to the async transport (or any other transport that you specify) and handle the messages that are received. The -vv option will enable verbose output that will show you the details of each message and handler. You can also use other options to control the behavior of the worker, such as the number of messages to handle, the memory limit, the signal handling, etc. You can see the full list of options by running:

php bin/console messenger:consume--help
Enter fullscreen modeExit fullscreen mode

You can also run multiple workers in parallel to increase the throughput of your queue system. You can use tools like Supervisor or Systemd to manage your workers and ensure that they are always running.

Conclusion

In this article, we have seen how to use the Messenger component to implement a simple queue system in Symfony using Redis as the transport layer. We have learned how to install the required packages, configure the transport, create the message and the handler, route the message to the transport, dispatch the message, and consume the message. The Messenger component provides a powerful and flexible way to send and receive messages between different parts of your application or between different applications. You can use it to implement various patterns, such as command bus, event bus, query bus, etc. You can also use different transports, such as AMQP, Doctrine, Amazon SQS, Google Pub/Sub, etc. You can find more information and examples in theofficial documentation.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Work
    PHP Laravel Backend Developer
  • Joined

More fromDaniyal Javani

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp