NestJs NATS listener

58 views Asked by At

Not so long ago I decided to deal with microservices, and came across a strange behavior in the context of nestjs, which is not described anywhere.

Let's say I have a basic microservice where the message broker is NATS.

// bootstrap method
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    TestModule,
    {
      transport: Transport.NATS,
      options: {
        servers: ['nats://remote-nats-1:4222', 'nats://remote-nats-2:4222'],
      },
    },
  );
  await app
    .listen()

And there's a conditional module with a controller:

// module
@Module({
  controllers: [TestController],
})
export class TestModule {}
// controller
@Controller()
export class TestController {
  @MessagePattern('message1')
  async message1() {}

  @MessagePattern('message2')
  async message2() {}
}

The problem is that mesage1 comes from one broker and message2 comes from another. And inside the controller only the address that is thrown to servers[0] when starting the service is listened to.

Has anyone encountered this problem, how did you overcome it?

I've double-checked the documentation several times, tried initializing the client inside the controller, specified additional options inside @MessagePattern.

0

There are 0 answers