New in Symfony 4.1: Messenger component

Contributed by
Samuel Roze
in #24411.

In Symfony 4.1, we added a new Messenger component that helps applications send and receive messages to/from other applications or via message queues. It provides a message bus and some routing capabilities to send messages in any service where you need it, like in a controller:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// src/Controller/DefaultController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function index(MessageBusInterface $bus)
    {
        // ...

        $bus->dispatch(new MyMessage());
    }
}

Sending messages is the first part of the process. Then you need to create a "message handler" and register it as a service with the messenger.message_handler tag to receive the message and do something with it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// src/MessageHandler/MyMessageHandler.php
namespace App\MessageHandler;

class MyMessageHandler
{
    public function __invoke(MyMessage $message)
    {
        // do something with the message
    }
}

Queues and AMQP adapters

The component includes an AMQP adapter ready to communicate with most of the popular AMQP brokers, such as RabbitMQ. The communication with queuing systems or third parties (Kafka, Amazon SQS, Google Pub/sub) is delegated to libraries like Enqueue's adapter:

1
2
3
4
5
# config/packages/messenger.yaml
framework:
    messenger:
        adapters:
            default: "amqp://guest:guest@localhost:5672/%2f/messages"

This config allows to route messages to the messenger.default_adapter and also configures a messenger.default_sender and messenger.default_receiver to be used when routing and consuming messages.

Routing

Instead of calling a handler, you can also route your messages to one or multiple senders:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# config/packages/messenger.yaml
framework:
    messenger:
        routing:
            # route this message to a single sender
            'My\Message\Message':  messenger.default_sender

            # route this message to multiple senders
            'My\Message\ToBeSentToTwoSenders': [messenger.default_sender, messenger.audit_sender]

            # route the rest of messages to the default sender
            '*': messenger.default_sender

Once your messages have been routed, you can consume them with the messenger:consume-messages command:

1
$ bin/console messenger:consume-messages messenger.default_receiver

The message bus defined by the Messenger component is based on middleware. You can read more about it and the rest of features of Messenger in the latest draft of its documentation.

Comments

Aww Yeah!
Looks great!

Is that related to enqueue/enqueue-bundle ? I saw a refrence on the bridge doc. Will it be deprecated for this component?
Yeah Great ^^
What an amazing news !
Thanks it will help us a lot!
Maybe it’s a mistake but it isn’t Amazon SQS ?
@Dequippe you are right! It's "Amazon SQS", not "Amazon SQL". Fixed. Thanks!
Hell yeah
"route this message to a singe sender"
single?
Greeat! thank you.
@Sébastien fixed! Thanks for the heads up.
Thanks a lot for (finally :p ) making this Messenger component!!
This component looks like a re-brand of SimpleBus. Some classes are just the exact copies of original Matthias's work. Or in SimpleBus we had MessageHandlerResolver, in new Messenger component it is called HandlerLocatorInterface.

I'm not trying to point a finger or something. This is OSS and it is fine to take ideas from other places, but what's the point of such a component? Why not to contribute to SimpleBus directly? There is Monolog, hence no need for Symfony/Logger. There is a Doctrine and noone wants Symfony/Db.

Moreover there is a queue-interop. New Messenger component could be just another implementation of it. Something like symfony/cache is an implementation of PSR-6.
Whaat a great news ! no more dealing with third libs :D
That's wonderful. When will the documentation be ready? I see there's still nothing. https://symfony.com/components/Messenger
This is fantastic, thanks!
I like that ! thanks for it
More great improvements, thank you!
Trying to use Messenger component. Get error:
Unrecognized option "adapters" under "framework.messenger"

Looking Configuration.php on master branch in section "messenger". No section with key adapters: framework-bundle/DependencyInjection/Configuration.php:966

What's wrong?
Login with SensioLabsConnect to post a comment