Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3c4fe2a

Browse filesBrowse files
committed
minor #19099 [Messenger] Added message serializer section (alexandre-daubois)
This PR was merged into the 5.4 branch. Discussion ---------- [Messenger] Added message serializer section Fix #17636 This is a common issue not documented yet. Closing this will help a lot I think! 😃 Commits ------- fa16632 [Messenger] Added message serializer section
2 parents bfd678e + fa16632 commit 3c4fe2a
Copy full SHA for 3c4fe2a

File tree

Expand file treeCollapse file tree

1 file changed

+92
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+92
-0
lines changed

‎messenger.rst

Copy file name to clipboardExpand all lines: messenger.rst
+92Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2570,6 +2570,98 @@ of the process. For each, the event class is the event name:
25702570
* :class:`Symfony\\Component\\Messenger\\Event\\WorkerStartedEvent`
25712571
* :class:`Symfony\\Component\\Messenger\\Event\\WorkerStoppedEvent`
25722572

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.