Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to specify different serializer for each Lambda / transport? #86

Open
Maxell92 opened this issue Apr 19, 2024 · 1 comment
Open

Comments

@Maxell92
Copy link
Contributor

We are processing messages from different producers in our Lambdas, so we need different Serializers. This is an example of our definition:

framework:
    messenger:

        transports:
            priority:
                dsn: '%env(xxx)%'
                serializer: messenger.transport.symfony_serializer
            dataUpdate:
                dsn: '%env(xxx)%'
                serializer: App\DataUpdate\DataUpdateMessageSerializer
            priceUpdate:
                dsn: '%env(xxx)%'
                serializer: App\PriceUpdate\PriceUpdateMessageSerializer

According to the documentation, I tried to specify a serializer for SqsConsumer. But how to add them there? It is possible to specify only 1 serializer there.

@starred-gijs
Copy link
Contributor

I have a similar use-case and I was able to get it working by separating the transports (SQS queues), linking to different lambdas, which have different handlers.

The following code snippets are incomplete!

serverless.yaml

functions:
  sqs-messenger-handler:
    handler: src/messenger-sqs.php
    description: 'SQS handler for Symfony Messenger'
    events:
      - sqs:
          arn:
            Fn::GetAtt: [SqsMessengerQueue, Arn]
          batchSize: 1   

  sqs-messenger-serializer:
    handler: src/messenger-sqs-symfony-serializer.php
    description: 'SQS handler for Symfony Messenger - with Symfony Serializer'
    events:
      - sqs:
          arn:
            Fn::GetAtt: [OtherSqsMessengerQueue, Arn]
          batchSize: 1

And in those php files, return a different service from the container
src/messenger-sqs.php

return $kernel->getContainer()->get('bref.sqs_consumer');

src/messenger-sqs-symfony-serializer.php

return $kernel->getContainer()->get('bref.sqs_consumer.symfony_serializer');

Which are defined to have different serializers injected:
services.yaml

services:
  bref.sqs_consumer:
    class: Bref\Symfony\Messenger\Service\Sqs\SqsConsumer
    public: true
    autowire: true
    arguments:
      $serializer: @Symfony\Component\Messenger\Transport\Serialization\SerializerInterface'

  bref.sqs_consumer.symfony_serializer:
    class: Bref\Symfony\Messenger\Service\Sqs\SqsConsumer
    public: true
    autowire: true
    arguments:
      $serializer: '@messenger.transport.symfony_serializer'

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants