Search code examples
rabbitmqnestjsnode-amqplib

Messages Patterns - RabbitMQ/NestJS


I'm trying to integrate to a project done using NestJS, a simple api where you can publish messages with a name (or pattern) and send it to that system that has implemented a handler that matches the name.

The system I'm building is really small, it wouldn't make much of a sense using NestJS for that.

The problem I'm having is the following:

I'm creating a simple api that triggers the publish of the message onto a queue. The consumer is on a system using NestJS.

I can't figure out how to give that messages a pattern that is recognized by that system. For example:

Let's say I want to publish a message that has a name of "CreateRecord" with a payload to be processed from the other system that has a handler with the same name with an implementation.

Using amqplib how do I give messages a name or pattern?


Solution

  • You can provide a pattern in the content object when you are publishing a message.

    publish(
      'my_exchange',
      'routing_key',
      { pattern: 'CreateRecord', data: 'Record' },
    );