@go1f/nestjs-kafka-client
TypeScript icon, indicating that this package has built-in type declarations

0.0.8 • Public • Published

NestJs Kafka Client build status coverage report

Description

A NestJS - KafkaJs Wrapper, wrapping on KafkaJS

Installation

$ npm install @go1f/nestjs-kafka-client

Add it to the NestJS app.module.ts or any module

import { KafkaModule } from '@go1f/nestjs-kafka-client';

const serviceConfig = {
  clientConfig:  {
    clientId: 'go1-node-app',     // consumer client id
      brokers: ['localhost:9092'] // kafka broker address
  },
  consumerConfig: { groupId: "something" } // consumer group id
};

@Module({
    imports: [KafkaModule.forRoot(serviceConfig)],
    controllers: [],
    providers: [],
})
export class Module {}

How to use in the Controller of Service

import { Controller, Get } from '@nestjs/common';
import { KafkaService, SubscribeTo } from '@go1f/nestjs-kafka-client';

@Controller()
export class AppController {
    // it will be injected by dependency injection
    constructor(private readonly kafkaClient: KafkaService) {}

    @Get()
    getHello(): string {
      this.kafkaClient.sendSingleMessage('testTopic','My first Kafka Message!');
    }

    @SubscribeTo('testTopic')
    handleMessage(message: any) {
      console.log(message);
    }
}

More complicated integrations

You have access to the consumer and producer object directly via these two functions, once you have the client reference. What you can do with those, is documented here: https://kafka.js.org/docs/

this.kafkaClient.getConsumer();
this.kafkaClient.getProducer();

Further Documentation and contribution

This kafka client is a shell for KafkaJs. Producer and consumers are exposed via get methods. If you want to do something fancy, refer to: https://kafka.js.org/docs/ The Wrapper is in very early stages. Feel free to create MRs for extension.

Maintainers

Readme

Keywords

none

Package Sidebar

Install

npm i @go1f/nestjs-kafka-client

Weekly Downloads

222

Version

0.0.8

License

MIT

Unpacked Size

418 kB

Total Files

30

Last publish

Collaborators

  • shangzhi