Get Kafka client for health-check in NestJs App

197 views Asked by At

I am consuming events using a nest.js app as microservice, I want to monitor the health of my application without creating a new client/connection different from the one in main.ts

 app.connectMicroservice<MicroserviceOptions>(
    {
      transport: Transport.KAFKA,
      options: {
        client: {
          brokers: [process.env.KAFKA_BROKER],
        },
        consumer: {
          groupId: process.env.KAFKA_GROUP_ID,
          retry: { retries: 3 },
        },
      },
    },
    { inheritAppConfig: true },
  );

In an ideal situation i can inject the kafkajs client instance created here in other parts of my app.

I created a class to extend ServerKafka and grab the private consumer as that is of the most interest to me:

export class ExtendedClientKafka extends ServerKafka {
  constructor(options: KafkaOptions['options']) {
    super(options);
  }

  get publicConsumer() {
    return this.consumer;
  }
}

and tried to use that class in main.ts to create the connection to my broker:

app.connectMicroservice({
    transport: Transport.KAFKA,
    options: new ExtendedClientKafka({
      client: {
        brokers: [process.env.KAFKA_BROKER],   
      },
      consumer: {
        groupId: process.env.KAFKA_GROUP_ID,
        retry: {
          retries: 3
        },
      },
    })
  }, { inheritAppConfig: true });

However when i try to start my application, i run into the following error:

TypeError: Cannot destructure property 'createPartitioner' of '(intermediate value)(intermediate value)(intermediate value)' as it is null.
    at Client.producer 
0

There are 0 answers