2

I am using Kafka.js in a Nest.js project. This is how I initialise KafkaClient:


@Module({
...
providers: [{
      provide: 'KAFKA_CLIENT',
      useFactory: async (configService: KafkaClientConfigService) => {

        const kafkaOptions = configService.getKafkaOptions();
        return ClientProxyFactory.create(kafkaOptions);
      },
      inject: [KafkaClientConfigService],
    },
  ]
...
})

Now I inject KafkaClient to my Controller, and I wish to consume messages with intervals. While there is a way to do it with Kafka.js using consumer.pause() I could not find any reference to such option in KafkaClient.

Is there any option to do so either by pausing or by throttling the consumer?

Yuvals
  • 3,094
  • 5
  • 32
  • 60

1 Answers1

0

The ClientKafka has a protected consumer property. On the consumer itself you can call pause() and resume().

To use these methods I'm currently extending the ClientKafka with a custom class and use it as provider instead.

import { ClientKafka, KafkaOptions } from '@nestjs/microservices'

export class KafkaClient extends ClientKafka {
  pause() {
    this.consumer.pause()
    // ...
  }
}
Tim
  • 2,236
  • 1
  • 20
  • 26