Download the PHP package vrok/messenger-reply without Composer
On this page you can find all versions of the php package vrok/messenger-reply. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vrok/messenger-reply
More information about vrok/messenger-reply
Files in vrok/messenger-reply
Package messenger-reply
Short Description Symfony messenger middleware & stamps to reply to messages
License MIT
Homepage https://vrok.de
Informations about the package messenger-reply
vrok/messenger-reply
This is a library to allow symfony/messenger to reply to messages with a result.
This is meant to be used in a setup with AMQP transport and two symfony instances
talking to each other over the same broker: e.g. a web frontend and a
microservice, where the frontend sends tasks to the service and requests a
reply, for example a generated PDF file.
Setup
Installation on both sides (You need the ReplyToStamp on the request side and the
middleware + stamp on the receiving side):
composer require vrok/messenger-reply
You need request and reply message classes that are equal on both sides, e.g. use a shared composer package:
Requesting side
Add a transport for the shared AMQP broker, routing to the input of the receiver (having the same exchange and queue name as the receivers input queue).
Add a transport for the shared AMQP broker, for receiving the replies (matching
the exchange and queue name of the receivers output queue):
We need separate transports as messenger:consume [transportname] consumes
all messages in all queues for that transport.
Route your requests to the shared transport/queue:
Replying side
Configure the middleware service:
Enable the middleware on your message bus:
(We have to disable the default middleware and explicitly define the order as
there is no priority option, just adding our service to the middleware-option
would add it before send_middleware. See https://github.com/symfony/symfony/issues/28568)
Configure an input transport where you send messages from the external
applications, which are consumed by your worker(s).
And an output transport where the replies are sent, optimally with one queue
for each application sending requests, so the only consume the
replies meant for them.
We need separate transports as messenger:consume [transportname] consumes
all messages in all queues for that transport.
All replies should be routed to the output transport:
Usage
Dispatch the request message with the attached ReplyStamp so the receiver knows where to send the replies:
Implement a MessageHandler that handles the requests and returns the reply Message object:
Consume requests (only on the input queue!) on the receiver side:
./bin/console messenger:consume input
Consume replies (only on the output queue!) on the requesting side:
./bin/console messenger:consume pdf-results
If you need to know on the requesting side which task to resume etc. with the
received reply, you can implement the Vrok\MessengerReply\TaskIdentifierMessageInterface
(and use the Vrok\MessengerReply\TaskIdentifierMessageTrait) on your request
and reply message classes to automatically transfer the task and/or identifier
properties given on the request to the reply. We cannot use stamps for this as
stamps are not accessible by MessageHandlers.