Roman
Roman

Reputation: 35

Hot to send message to all queue subscribers?

I have two microservices. First microservice it's front server, which requests for some work in one of second microservice instance. The great solution of NATS transports is queueing. I can send message and NATS will send it only to one of instance. But that if i need to send message, or event to every queue instances?

Front service:


//module

  providers: [
    ...,

    {
      provide: 'CLIENT_PROXY_FACTORY',
      useFactory: (configService: ConfigService) => {
        return ClientProxyFactory.create(configService.get('microservice'));
      },
      inject: [ConfigService],
    }
  ],
// service

constructor(
    @Inject('CLIENT_PROXY_FACTORY')
    private nats: ClientProxy,
) {

}
// in some method
method() {

  this.nats.emit('test', 0);
  this.nats.send('test1', true).toPromise();

}

Worker service:

// bootstrap

  transport: Transport.NATS,
  options: {
    url: 'nats://127.0.0.1:4222',
    queue: 'worker'
  }
// controller

  @MessagePattern('test')
  messagefunc() {
    console.log('Got message!');
  }

  @EventPattern('test1')
  eventfunc() {
    console.log('Got event!');
  }

So. In my example i need to create 2 or more instances of Worker service. And i need an solution how to sometimes deliver events or messages to all Worker service instances?

Upvotes: 0

Views: 1182

Answers (1)

Georg
Georg

Reputation: 982

Make the worker services subscribe to two subjects:

  • Subject A in a queue group. Messages published to A will be sent only to one worker.
  • Subject B without queue group. Messages published to B will be sent to all workers.

Sorry, can't tell how to implement this in NestJS.

Upvotes: 1

Related Questions