Warning: You are browsing the documentation for Symfony 4.1, which is no longer maintained.

Read the updated version of this page for Symfony 5.3 (the current stable version).

The Messenger Component

4.1 version

The Messenger Component

The Messenger component helps applications send and receive messages to/from other applications or via message queues.

The component is greatly inspired by Matthias Noback’s series of blog posts about command buses and the SimpleBus project.

See also

This article explains how to use the Messenger features as an independent component in any PHP application. Read the How to Use the Messenger article to learn about how to use it in Symfony applications.


$ composer require symfony/messenger

Alternatively, you can clone the https://github.com/symfony/messenger repository.


If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.


Responsible for serializing and sending messages to something. This something can be a message broker or a third party API for example.
Responsible for retrieving, deserializing and forwarding messages to handler(s). This can be a message queue puller or an API endpoint for example.
Responsible for handling messages using the business logic applicable to the messages. Handlers are called by the HandleMessageMiddleware middleware.
Middleware can access the message and its wrapper (the envelope) while it is dispatched through the bus. Literally “the software in the middle”, those are not about core concerns (business logic) of an application. Instead, they are cross cutting concerns applicable throughout the application and affecting the entire message bus. For instance: logging, validating a message, starting a transaction, … They are also responsible for calling the next middleware in the chain, which means they can tweak the envelope, by adding items to it or even replacing it, as well as interrupt the middleware chain.
Messenger specific concept, it gives full flexibility inside the message bus, by wrapping the messages into it, allowing to add useful information inside through envelope items.
Envelope Items
Piece of information you need to attach to your message: serializer context to use for transport, markers identifying a received message or any sort of metadata your middleware or transport layer may use.


The bus is used to dispatch messages. The behavior of the bus is in its ordered middleware stack. The component comes with a set of middleware that you can use.

When using the message bus with Symfony’s FrameworkBundle, the following middleware are configured for you:

  1. Symfony\Component\Messenger\Middleware\LoggingMiddleware (logs the processing of your messages)
  2. Symfony\Component\Messenger\Asynchronous\Middleware\SendMessageMiddleware (enables asynchronous processing)
  3. Symfony\Component\Messenger\Middleware\HandleMessageMiddleware (calls the registered handler(s))


use App\Message\MyMessage;
use Symfony\Component\Messenger\MessageBus;
use Symfony\Component\Messenger\Handler\Locator\HandlerLocator;
use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware;

$bus = new MessageBus([
    new HandleMessageMiddleware(new HandlerLocator([
        MyMessage::class => $handler,

$bus->dispatch(new MyMessage(/* ... */));


Every middleware needs to implement the Symfony\Component\Messenger\Middleware\MiddlewareInterface.


Once dispatched to the bus, messages will be handled by a “message handler”. A message handler is a PHP callable (i.e. a function or an instance of a class) that will do the required processing for your message:

namespace App\MessageHandler;

use App\Message\MyMessage;

class MyMessageHandler
   public function __invoke(MyMessage $message)
       // Message processing...

Adding Metadata to Messages (Envelopes)

If you need to add metadata or some configuration to a message, wrap it with the Symfony\Component\Messenger\Envelope class. For example, to set the serialization groups used when the message goes through the transport layer, use the SerializerConfiguration envelope:

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration;

    (new Envelope($message))->with(new SerializerConfiguration([
        'groups' => ['my_serialization_groups'],

At the moment, the Symfony Messenger has the following built-in envelope items:

  1. Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration, to configure the serialization groups used by the transport.
  2. Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration, to configure the validation groups used when the validation middleware is enabled.
  3. Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage, an internal item that marks the message as received from a transport.

Instead of dealing directly with the messages in the middleware you can receive the envelope by implementing the Symfony\Component\Messenger\EnvelopeAwareInterface marker, like this:

use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\EnvelopeAwareInterface;

class MyOwnMiddleware implements MiddlewareInterface, EnvelopeAwareInterface
    public function handle($envelope, callable $next)
        // $envelope here is an `Envelope` object, because this middleware
        // implements the EnvelopeAwareInterface interface.

        if (null !== $envelope->get(ReceivedMessage::class)) {
            // Message just has been received...

            // You could for example add another item.
            $envelope = $envelope->with(new AnotherEnvelopeItem(/* ... */));

        return $next($envelope);

The above example will forward the message to the next middleware with an additional envelope item if the message has just been received (i.e. has the ReceivedMessage item). You can create your own items by implementing Symfony\Component\Messenger\EnvelopeAwareInterface.


Any envelope item must be php serializable if going through transport using the Symfony\Component\Messenger\Transport\Serialization\Serializer base serializer.


In order to send and receive messages, you will have to configure a transport. A transport will be responsible for communicating with your message broker or 3rd parties.

Your own Sender

Using the Symfony\Component\Messenger\Transport\SenderInterface, you can create your own message sender. Imagine that you already have an ImportantAction message going through the message bus and being handled by a handler. Now, you also want to send this message as an email.

First, create your sender:

namespace App\MessageSender;

use App\Message\ImportantAction;
use Symfony\Component\Messenger\Transport\SenderInterface;
use Symfony\Component\Messenger\Envelope;

class ImportantActionToEmailSender implements SenderInterface
   private $mailer;
   private $toEmail;

   public function __construct(\Swift_Mailer $mailer, string $toEmail)
       $this->mailer = $mailer;
       $this->toEmail = $toEmail;

   public function send(Envelope $envelope)
       $message = $envelope->getMessage();

       if (!$message instanceof ImportantAction) {
           throw new \InvalidArgumentException(sprintf('This transport only supports "%s" messages.', ImportantAction::class));

           (new \Swift_Message('Important action made'))
                   '<h1>Important action</h1><p>Made by '.$message->getUsername().'</p>',

Your own Receiver

A receiver is responsible for getting messages from a source and dispatching them to the application.

Imagine you already processed some “orders” in your application using a NewOrder message. Now you want to integrate with a 3rd party or a legacy application but you can’t use an API and need to use a shared CSV file with new orders.

You will read this CSV file and dispatch a NewOrder message. All you need to do is to write your custom CSV receiver and Symfony will do the rest.

First, create your receiver:

namespace App\MessageReceiver;

use App\Message\NewOrder;
use Symfony\Component\Messenger\Transport\ReceiverInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Messenger\Envelope;

class NewOrdersFromCsvFileReceiver implements ReceiverInterface
   private $serializer;
   private $filePath;

   public function __construct(SerializerInterface $serializer, string $filePath)
       $this->serializer = $serializer;
       $this->filePath = $filePath;

   public function receive(callable $handler): void
       $ordersFromCsv = $this->serializer->deserialize(file_get_contents($this->filePath), 'csv');

       foreach ($ordersFromCsv as $orderFromCsv) {
           $order = new NewOrder($orderFromCsv['id'], $orderFromCsv['account_id'], $orderFromCsv['amount']);

           $handler(new Envelope($order));

   public function stop(): void
       // noop

Receiver and Sender on the same Bus

To allow sending and receiving messages on the same bus and prevent an infinite loop, the message bus will add a Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage envelope item to the message envelopes and the Symfony\Component\Messenger\Asynchronous\Middleware\SendMessageMiddleware middleware will know it should not route these messages again to a transport.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.