Skip to content

Commit fa16632

Browse files
[Messenger] Added message serializer section
1 parent 6fb8828 commit fa16632

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

messenger.rst

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,6 +2566,98 @@ of the process. For each, the event class is the event name:
25662566
* :class:`Symfony\\Component\\Messenger\\Event\\WorkerStartedEvent`
25672567
* :class:`Symfony\\Component\\Messenger\\Event\\WorkerStoppedEvent`
25682568

2569+
Message Serializer For Custom Data Formats
2570+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2571+
2572+
It is likely that you receive messages from other applications that are not
2573+
exactly in the format you need. Not all applications will return a JSON message
2574+
with ``body`` and ``headers`` fields. In this case, you'll need to
2575+
create a new message serializer. This can be done by implementing the
2576+
:class:`Symfony\\Component\\Messenger\\Transport\\Serialization\\SerializerInterface`.
2577+
Let's say you want to create a message decoder::
2578+
2579+
namespace App\Messenger\Serializer;
2580+
2581+
use Symfony\Component\Messenger\Envelope;
2582+
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
2583+
2584+
class MessageWithTokenDecoder implements SerializerInterface
2585+
{
2586+
public function decode(array $encodedEnvelope): Envelope
2587+
{
2588+
$envelope = \json_decode($encodedEnvelope, true);
2589+
2590+
try {
2591+
// parse the data you received with your custom fields
2592+
$data = $envelope['data'];
2593+
$data['token'] = $envelope['token'];
2594+
2595+
// other operations like getting information from stamps
2596+
} catch (\Throwable $throwable) {
2597+
// wrap any exception that may occur in the envelope to send it to the failure transport
2598+
return new Envelope($throwable);
2599+
}
2600+
2601+
return new Envelope($data);
2602+
}
2603+
2604+
public function encode(Envelope $envelope): array
2605+
{
2606+
// this decoder does not encode messages, but you can implement it by returning
2607+
// an array with serialized stamps if you need to send messages in a custom format
2608+
throw new \LogicException('This serializer is only used for decoding messages.');
2609+
}
2610+
}
2611+
2612+
Now that this serializer is created, you can use it in your transport:
2613+
2614+
.. configuration-block::
2615+
2616+
.. code-block:: yaml
2617+
2618+
# config/packages/messenger.yaml
2619+
framework:
2620+
messenger:
2621+
transports:
2622+
my_transport:
2623+
dsn: '%env(MY_TRANSPORT_DSN)%'
2624+
serializer: 'App\Messenger\Serializer\MessageWithTokenDecoder'
2625+
2626+
.. code-block:: xml
2627+
2628+
<!-- config/packages/messenger.xml -->
2629+
<?xml version="1.0" encoding="UTF-8" ?>
2630+
<container xmlns="http://symfony.com/schema/dic/services"
2631+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2632+
xmlns:framework="http://symfony.com/schema/dic/symfony"
2633+
xsi:schemaLocation="http://symfony.com/schema/dic/services
2634+
https://symfony.com/schema/dic/services/services-1.0.xsd
2635+
http://symfony.com/schema/dic/symfony
2636+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
2637+
2638+
<framework:config>
2639+
<framework:messenger>
2640+
<framework:transport name="my_transport" dsn="%env(MY_TRANSPORT_DSN)%" serializer="App\Messenger\Serializer\MessageWithTokenDecoder">
2641+
<!-- ... -->
2642+
</framework:transport>
2643+
</framework:messenger>
2644+
</framework:config>
2645+
</container>
2646+
2647+
.. code-block:: php
2648+
2649+
// config/packages/messenger.php
2650+
use App\Messenger\Serializer\MessageWithTokenDecoder;
2651+
use Symfony\Config\FrameworkConfig;
2652+
2653+
return static function (FrameworkConfig $framework): void {
2654+
$messenger = $framework->messenger();
2655+
2656+
$messenger->transport('my_transport')
2657+
->dsn('%env(MY_TRANSPORT_DSN)%')
2658+
->serializer(MessageWithTokenDecoder::class);
2659+
};
2660+
25692661
Multiple Buses, Command & Event Buses
25702662
-------------------------------------
25712663

0 commit comments

Comments
 (0)