1.0.5 • Published 3 years ago

nestjs-rdkafka v1.0.5

Weekly downloads
8
License
MIT
Repository
github
Last release
3 years ago

nestjs-rdkafka

NPM

npm version Build Status npm npm bundle size
Maintainability Test Coverage

Description

A NestJS module wrapper for node-rdkafka.

Installation

npm i nestjs-rdkafka

Basic usage

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 */
  }
}

Disconnect

All clients will be automatically disconnected from Kafka onModuleDestroy. You can manually disconnect by calling:

await this.kafkaService.disconnect();

License

nestjs-rdkafka is MIT licensed.

1.0.5

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.0

3 years ago