A NestJS module wrapper for node-rdkafka.
npm i nestjs-rdkafka
Initialize module with configuration of consumer
, producer
or admin client
respectively. A full list of configuration can be found on node-rdkafka
's Configuration section.
app.module.ts
import { Module } from "@nestjs/common";
import { TypegooseModule } from "nestjs-typegoose";
import { CatsModule } from "./cat.module.ts";
@Module({
imports: [
KafkaModule.forRootAsync({
consumer: {
conf: {
'group.id': 'kafka_consumer',
'metadata.broker.list': '127.0.0.1:9092'
}
},
producer: {
conf: {
'client.id': 'kafka_prducer',
'metadata.broker.list': '127.0.0.1:9092'
}
},
adminClient: {
conf: {
'metadata.broker.list': '127.0.0.1:9092'
}
}
}),
CatsModule,
],
})
export class ApplicationModule {}
Inject the kafka.service
in other provider/service to get the consumer, producer and or client.
cats.service.ts
import { Injectable } from "@nestjs/common";
import { KafkaService } from '@a97001/nestjs-rdkafka';
@Injectable()
export class CatsService {
constructor(
private readonly kafkaService: KafkaService
) {
const consumer = this.kafkaService.getConsumer(); // consumer
const producer = this.kafkaService.getProducer(); // producer
const adminClient = this.kafkaService.getAdminClient(); // admin client
/* Throw Error if you get any of these without configuration in module initialization */
}
}
All clients will be automatically disconnected from Kafka onModuleDestroy
. You can manually disconnect by calling:
await this.kafkaService.disconnect();
nestjs-rdkafka is MIT licensed.