0.0.8 • Published 6 years ago

@go1f/nestjs-kafka-client v0.0.8

Weekly downloads
370
License
MIT
Repository
-
Last release
6 years ago

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

  • Shangzhi Pan (shangzhi.pan@go1.com)