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 🙏

© 2026 – Pkg Stats / Ryan Hefner

kafka-consumer-host

v2.2.0

Published

A NestJS Kafka consumer host module for scalable message consumption.

Readme

📦 kafka-consumer-host

A NestJS module that provides a plug-and-play Kafka consumer setup, making it easy to build Kafka consumers without boilerplate. Designed for scalability and simplicity, it supports automatic handler registration using a clean OOP pattern.


🚀 Features

  • ✅ Quick setup with KafkaConsumerHostModule.register()
  • ✅ Class-based Kafka handlers using KafkaHandlerBase
  • ✅ Supports multiple topics and handlers
  • ✅ Full NestJS DI support
  • ✅ Clean logging & message metadata (topic, partition, offset)
  • ✅ Automatic JSON parsing (fallback to raw string)
  • ✅ Powered by kafkajs

📦 Installation

npm install kafka-consumer-host

⚙️ Usage

1️⃣ Register the Kafka consumer in your AppModule

import { Module } from "@nestjs/common";
import {
  KafkaConsumerHostModule,
  KafkaConsumerOffsetReset,
} from "kafka-consumer-host";
import { YourHandlerService } from "./your-handler.service";

@Module({
  imports: [
    KafkaConsumerHostModule.register({
      bootstrapServer: "localhost:9092",
      groupId: "your-consumer-group",
      clientId: "your-client-id",
      offsetReset: KafkaConsumerOffsetReset.LATEST, // or KafkaConsumerOffsetReset.EARLIEST
    }),
  ],
  providers: [YourHandlerService],
})
export class AppModule {}

2️⃣ Create your Kafka handler service

import { Injectable } from "@nestjs/common";
import { KafkaHandlerBase } from "kafka-consumer-host";

@Injectable()
export class YourHandlerService extends KafkaConsumerBase {
  registerHandlers(): void {
    this.handle("your-topic-1", this.handleTopic1);
    this.handle("your-topic-2", this.handleTopic2);
  }

  async handleTopic1(payload: any, metadata: any): Promise<void> {
    console.log("Received message for your-topic-1:", payload, metadata);
    // Process your message here...
  }

  async handleTopic2(payload: any, metadata: any): Promise<void> {
    console.log("Received message for your-topic-2:", payload, metadata);
    // Process your message here...
  }
}

🛠 Handler Parameters

| Parameter  | Type     | Description                                                    |
| ---------- | -------- | -------------------------------------------------------------- |
| `payload`  | `any`    | Parsed Kafka message payload (JSON or raw string if not JSON)  |
| `metadata` | `object` | Includes topic, partition, offset, timestamp, key, and headers |

Example metadata:

{
  "topic": "your-topic-1",
  "partition": 0,
  "offset": "42",
  "timestamp": "1623762345678",
  "key": "customer-123",
  "headers": { "correlationId": "abc-123" }
}

🔑 Configuration Options

| Option            | Type                       | Required | Description                                                         |
| ----------------- | -------------------------- | -------- | ------------------------------------------------------------------- |
| `bootstrapServer` | `string \| string[]`       | ✅       | Kafka broker(s)                                                     |
| `groupId`         | `string`                   | ✅       | Kafka consumer group ID                                             |
| `clientId`        | `string`                   | ✅       | Kafka client ID                                                     |
| `offsetReset`     | `KafkaConsumerOffsetReset` | ✅       | Start from `'earliest'` or `'latest'` if no committed offset exists |

🧠 How It Works

  • 🏁 On app startup:
    • Initializes the Kafka consumer
    • Waits for all handlers to be registered
    • Subscribes to all registered topics
  • 📩 When a message is received:
    • Automatically parses JSON payload (or provides raw string)
    • Calls your registered handler with payload + metadata

⚠️ Important Notes

  • ✅ Handlers are registered via KafkaConsumerBase; no manual registration needed.
  • ✅ Ensure your handler service is added to the providers array for DI.
  • 🔄 To maintain message ordering, use a message key when producing so Kafka routes related messages to the same partition.

🤝 Contributing

PRs and ideas are welcome—feel free to open issues or suggest improvements!

💬 Questions?

Open an issue or start a discussion.