Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to define queue name while creating microservice?

I want to have a hybrid NestJS app, HTTP + RabbitMQ. But don't get if I should create different microservices for each queue.

I have followed NestJS' RabbitMQ guide (https://docs.nestjs.com/microservices/rabbitmq) and GitHub example (https://github.com/nestjs/nest/tree/master/sample/03-microservices).

main.ts

app.connectMicroservice({
    transport: Transport.RMQ,
    options: {
      urls: [`amqp://user:user@hostname:5672`],
      queue: "cats_queue",
      queueOptions: { durable: false },
      prefetchCount: 1,
    }
  });
...
await app.startAllMicroservicesAsync();

app.module.ts (module import)

ClientsModule.register([{
      name: "CATS_QUEUE", transport: Transport.RMQ, options: {
        urls: [`amqp://user:user@hostname:5672`],
        queue: "cats_queue",
        queueOptions: { durable: false },
        prefetchCount: 1
      }
    }])

app.controller.ts

constructor(
    @Inject("CATS_QUEUE") private readonly client: ClientProxy
  ) {
  }
  @Get("mq")
  mq(): Observable<number> {
    const pattern = { cmd: "sum" };
    const data = [1, 2, 3, 4, 5];
    return this.client.send<number>(pattern, data);
  }

  @MessagePattern({ cmd: "sum" })
  sum(data: number[]): number {
    console.log("MESSAGE RECEIVED : " + data.toString());
    return (data || []).reduce((a, b) => a + b);
  }

As I understood, I need to define ClientsModule.register() for each queue in app.module.ts. But why I also need to define RabbitMQ queue name while creating microservice? Do I need to create different microservices for each queue?

like image 365
Eray Avatar asked Mar 18 '26 10:03

Eray


1 Answers

I'm not a NestJS user, but it seems logical that when you use queues to send messages between your microservices, you will need a queue for each microservice (not the other way around).

Queues are used to send (TO) and receive(Process) messages between independent components asynchronously.

Does that make sense?

like image 145
Sean Farmar Avatar answered Mar 22 '26 12:03

Sean Farmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!