rabbitmqnestjskrakend

RabbitMQ/Krakend/NestJS - How can I post the consumed message through Krakend API's async agent?


This is my config for krakend.json

  "async_agent": [
    {
      "name": "test-agent",
      "connection": {
        "max_retries": 10,
        "backoff_strategy": "exponential-jitter"
      },
      "consumer": {
        "topic": "krakend",
        "workers": 5
      },
      "backend": [
        {
          "url_pattern": "/greeted",
          "method": "POST",
          "host": [ "http://127.0.0.1:2999" ],
          "disable_host_sanitize": false
        }
      ],
      "extra_config": {
        "async/amqp": {
          "host": "amqp://guest:guest@localhost:5672/",
          "name": "krakend",
          "exchange": "ApiGatewayExchange",
          "durable": true,
          "delete": false,
          "exclusive": false,
          "no_wait": false,
          "auto_ack": false
        }
      }
    }
  ]

Messages are sent from service-a like so:

export class AppService {
  constructor(@Inject('GREETING_SERVICE') private client: ClientProxy){}

  getHello(): ResponseDTO {
    const responseDTO: ResponseDTO = {
      action: 'Hello',
      service: 'from service A'
    }
    this.client.emit('', responseDTO);
    return responseDTO;
  }
}

And imported GREETING_SERVICE config like so:

  imports: [
    ClientsModule.register([
      {
        name: 'GREETING_SERVICE',
        transport: Transport.RMQ,
        options: {
          urls: ['amqp://test:test@localhost:5672/'],
          queue: 'krakend'
        }
      }
    ])
  ],

Lastly, this is the endpoint in another service (let's call this service-c) that gets that message from the consumer:

  @Post('greeted')
  TestHello(@Body() data: any) {
    console.log(data)
    return data
  }

The message is successfully consumed as set by the async_agent in my krakend file, but the message isn't posted as a body to that endpoint. When I did a console.log of that data supposedly passed, it just prints {}.

Am I doing anything wrong here? Been scratching my head for hours.


Solution

  • The async part of your krakend.json configuration looks good to me, but I am suspecting about the problem you might have.

    Most of the javascript frameworks today will require you to pass specific headers to work their magic like Content-Type or Accept. You have to take into account that KrakenD will pass a very reduced set of headers to your NestJS application (Accept-Encoding and User-Agent as far as I can remember).

    I am unfamiliar with NestJS, but I would bet that you need to pass the Content-Type and you are good to go. Here's my suggestion of configuration:

        "async_agent": [
            {
                "name": "test-agent",
                "connection": {
                    "max_retries": 10,
                    "backoff_strategy": "exponential-jitter"
                },
                "consumer": {
                    "topic": "krakend",
                    "workers": 5
                },
                "backend": [
                    {
                        "url_pattern": "/greeted",
                        "method": "POST",
                        "host": [
                            "http://127.0.0.1:2999"
                        ],
                        "disable_host_sanitize": false,
                        "extra_config": {
                            "modifier/martian": {
                                "header.Modifier": {
                                    "scope": [
                                        "request"
                                    ],
                                    "name": "Content-Type",
                                    "value": "application/json"
                                }
                            }
                        }
                    }
                ],
                "extra_config": {
                    "async/amqp": {
                        "host": "amqp://guest:guest@localhost:5672/",
                        "name": "krakend",
                        "exchange": "ApiGatewayExchange",
                        "durable": true,
                        "delete": false,
                        "exclusive": false,
                        "no_wait": false,
                        "auto_ack": false
                    }
                }
            }
        ]
    }
    

    Hope this helps