kafka-consumer-host
Version:
A NestJS Kafka consumer host module for scalable message consumption.
132 lines (101 loc) β’ 4.36 kB
Markdown
# π¦ 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](https://kafka.js.org)
---
## π¦ Installation
```bash
npm install kafka-consumer-host
```
## βοΈ Usage
### 1οΈβ£ Register the Kafka consumer in your AppModule
```ts
import { Module } from "@nestjs/common";
import {
KafkaConsumerHostModule,
KafkaConsumerOffsetReset,
} from "kafka-consumer-host";
import { YourHandlerService } from "./your-handler.service";
export class AppModule {}
```
### 2οΈβ£ Create your Kafka handler service
```ts
import { Injectable } from "@nestjs/common";
import { KafkaHandlerBase } from "kafka-consumer-host";
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
```markdown
| 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:
```json
{
"topic": "your-topic-1",
"partition": 0,
"offset": "42",
"timestamp": "1623762345678",
"key": "customer-123",
"headers": { "correlationId": "abc-123" }
}
```
### π Configuration Options
```markdown
| 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.