Search code examples
controllernestjsamazon-sqs

Why @ssut/nestjs-sqs should be used in a service file instead of a controller?


I'm starting to work with @ssut/nestjs-sqs and I'm a bit confused about how it works.
By the documentation, you need to have this code in a provider:

        @SqsMessageHandler(/** name: */ 'queueName', /** batch: */ false)
        public async handleMessage(message: AWS.SQS.Message) {  
         // Logic to handle the message
        }

I'll would like to have the message consumed in a controller and then I'll send the data to a provider. But if I try to had this method into a controller, I get

[SqsService] No metadata found for: <my_sqs_queue>

Why does the messageHandler method only works in a Service file and not in the controller?


Solution

  • It's due to how Nest's DiscoveryService works and how the SqsService makes use of the DiscoveryService. There is a Discover#providerMethodsWithMetaAtKey method that only looks at providers, so when you add that decorator to a controller, the discovery service won't ever see it and the sqs service won't be able to act upon it.