Skip to content

Latest commit

 

History

History
96 lines (80 loc) · 1.66 KB

README.md

File metadata and controls

96 lines (80 loc) · 1.66 KB

NestJS's Dynamic Module for Producer.

Install

npm install --save @kafka-ts/nestjs-producer

# or

yarn add @kafka-ts/nestjs-producer

# or

pnpm add @kafka-ts/nestjs-producer

Usage

// producer.module.ts
import { Module } from '@nestjs/common';
import { KafkaProducer } from '@kafka-ts/nestjs-producer';

import { ProducerController } from './producer.controller';

@Module({
  imports: [
    KafkaProducer.register([
      {
        brokers: ['localhost:9092'],
      },
    ]),
  ],
  controllers: [ProducerController],
})
export class ProducerModule {}
// producer.controller.ts
import { Controller, Get } from '@nestjs/common';
import { KafkaModel } from '@kafka-ts/nestjs-producer';

@Controller('/producer')
export class ProducerController {
  constructor(private readonly kafkaModel: KafkaModel) {}

  @Get()
  public async publish(): Promise<string> {
    this.kafkaModel
      .publish({
        topicMessages: [
          {
            topic: 'topic',
            messages: [
              {
                value: 'hello',
              },
            ],
          },
        ],
      })
      .then((data) => {
        console.log(data);
      })
      .catch((err) => {
        console.log(err);
      });

    return 'Ok!';
  }

  @Get('message')
  public async publishMessage(): Promise<string> {
    this.kafkaModel
      .publish({
        topicMessages: {
          topic: 'topic_2',
          messages: [
            {
              value: 'hi',
            },
          ],
        },
      })
      .then((data) => {
        console.log(data);
      })
      .catch((err) => {
        console.log(err);
      });

    return 'Ok!';
  }
}