Symfony how to use Mercure with Messenger + RabbitMQ?

1.1k views Asked by At

I am looking to use Mercury with RabbitMQ. This is the first time that I have used Mercury as well as RabbitMQ so I am not yet good.

Here is where I am:

I've installed Mercure, and Messenger.

Messenger.yaml

framework:
    messenger:
        # Uncomment this (and the failed transport below) to send failed messages to this transport for later handling.
        failure_transport: failed

        transports:
            # https://symfony.com/doc/current/messenger.html#transport-configuration
             async: '%env(MESSENGER_TRANSPORT_DSN)%'
             failed: '%env(MESSENGER_TRANSPORT_FAILED_DSN)%'
            # sync: 'sync://'

        routing:
            # Route your messages to the transports
            # 'App\Message\YourMessage': async

.env:

MERCURE_PUBLISH_URL=http://localhost:3000/.well-known/mercure
MERCURE_JWT_TOKEN=aVerySecretKey
MESSENGER_TRANSPORT_DSN=amqp://bastien:mypassword@localhost:5672/%2f/messages
MESSENGER_TRANSPORT_FAILED_DSN=amqp://bastien:mypassword@localhost:5672/%2f/failed

And in my controller I simulated 50 pings on a URL of my local app:

    /**
     * @Route("/ping", name="ping", methods={"POST"})
     */
    public function ping(MessageBusInterface $bus)
    {
        for($i=0;$i<=50;$i++)
        {
            $update = new Update("http://monsite.com/ping", "[]");
            $bus->dispatch($update);
        }
        return $this->redirectToRoute('home');
    }

I have successfully started my instance of Mercury as well as that of Messenger which is therefore well connected to my RabbitMQ.

But when I test sending the pings, it works, but without going through my RabbitMQ. Did I miss something? I think of my Messenger.yaml in the routing part but I don't know what to put if it is the case

1

There are 1 answers

2
G1.3 On

By default, messages are executed synchronously in messenger.

You will need to configure the Update message in the messenger.yaml to use the async transport:

Symfony\Component\Mercure\Update: async