New in Symfony 4.1: Messenger component
March 28, 2018 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
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.
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Is that related to enqueue/enqueue-bundle ? I saw a refrence on the bridge doc. Will it be deprecated for this component?
Maybe it’s a mistake but it isn’t Amazon SQS ?
single?
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.
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?