npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@choewy/nestjs-kafka

v0.1.9

Published

NestJS Kafka

Downloads

20

Readme

NestJS Kafka

Installing

npm i @choewy/nestjs-kafka

app.enableShutdownHooks() is essential when using the consumer

Options

Kafka Producer Options

import { ProducerConfig } from 'kafkajs';

export interface KafkaProducerOptions extends ProducerConfig {
  use: boolean;
  logging?: boolean;
}

Kafka Admin Options

import { AdminConfig } from 'kafkajs';

export interface KafkaAdminOptions extends AdminConfig {
  use: boolean;
}

Kafka Consumer Options

import { ConsumerConfig, ConsumerRunConfig, ConsumerSubscribeTopics } from 'kafkajs';

export interface KafkaConsumerOptions extends ConsumerConfig {
  subscriptions?: ConsumerSubscribeTopics;
  runOptions?: Omit<ConsumerRunConfig, 'eachMessage' | 'eachBatch'>;
  logging?: boolean;
}

Kafka Module Options

import { KafkaConfig } from 'kafkajs';

import { KafkaAdminOptions } from './kafka-admin-options.interface';
import { KafkaConsumerOptions } from './kafka-consumer-options.interface';
import { KafkaProducerOptions } from './kafka-producer-options.interface';

export interface KafkaModuleOptions extends KafkaConfig {
  consumer?: KafkaConsumerOptions;
  producer?: KafkaProducerOptions;
  admin?: KafkaAdminOptions;
  global?: boolean;
}

Uses

Producer

import { Module } from '@nestjs/common';
import { KafkaModule } from '@choewy/nestjs-kafka';

@Module({
  import: [
    KafkaModule.register({
      producer: { use: true },
    }),
  ],
})
export class AppModule {}

Consumer

import { Module, OnModuleInit } from '@nestjs/common';
import { KafkaModule, KafkaConsumer } from '@choewy/nestjs-kafka';

@Module({
  import: [
    KafkaModule.register({
      clientId: 'app-1',
      consumer: { groupId: 'group-1' },
    }),
  ],
})
export class AppModule implements OnModuleInit {
  constructor(private readonly consumer: KafkaConsumer) {}

  async onModuleInit() {
    await this.consumer.subscribe({ topics: ['topic-1', 'topic-2'] });
    await this.consumer.run();
  }
}

KafkaConsumer can subscribe topics without OnModuleInit using the consumer.subscriptions option.

import { Module } from '@nestjs/common';
import { KafkaModule, KafkaConsumer } from '@choewy/nestjs-kafka';

@Module({
  import: [
    KafkaModule.register({
      clientId: 'app-1',
      consumer: {
        groupId: 'group-1',
        subscriptions: { topics: ['topic-1', 'topic-2'] },
      },
    }),
  ],
})
export class AppModule {}

Example

Run kafak with docker

npm run docker

Run Nest.js Application

cd example

npm run start:dev
/** @filename example/src/producer/producer.module.ts */

import { Module } from '@nestjs/common';
import { KafkaModule } from '@choewy/nestjs-kafka';

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

@Module({
  imports: [
    KafkaModule.register({
      clientId: 'kafka-consumer',
      brokers: ['localhost:29092'],
      producer: { use: true, allowAutoTopicCreation: true },
    }),
  ],
  controllers: [ProducerController],
  providers: [ProducerService],
})
export class ProducerModule {}
/** @filename example/src/consumer/consumer.module.ts */

import { Module } from '@nestjs/common';
import { KafkaModule } from '@choewy/nestjs-kafka';

import { ConsumerService } from './consumer.service';

@Module({
  imports: [
    KafkaModule.register({
      clientId: 'kafka-consumer',
      brokers: ['localhost:29092'],
      consumer: {
        groupId: 'consumer-group-1',
        subscriptions: {
          topics: ['message-topic-1', 'message-topic-2', 'message-topic-3'],
        },
      },
    }),
  ],
  providers: [ConsumerService],
})
export class ConsumerModule {}
# send a message(Buffer("hi, message topic 1")) to message-topic-1
curl http://localhost:3000/producer/message/1

# send a message(String("hi, message topic 2")) to message-topic-2
curl http://localhost:3000/producer/message/2

# send a message(String('{"message": "hi, message topic 3"}')) to message-topic-3
curl http://localhost:3000/producer/message/3