Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nest-cn/nestjs-kafka
A NestJS - KafkaJs Wrapper, wrapping on KafkaJS
https://github.com/nest-cn/nestjs-kafka
Last synced: 29 days ago
JSON representation
A NestJS - KafkaJs Wrapper, wrapping on KafkaJS
- Host: GitHub
- URL: https://github.com/nest-cn/nestjs-kafka
- Owner: nest-cn
- Created: 2021-11-29T15:49:41.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-03-19T07:30:08.000Z (almost 2 years ago)
- Last Synced: 2024-12-01T20:45:43.494Z (about 1 month ago)
- Language: TypeScript
- Size: 11.7 KB
- Stars: 9
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
## NestJs Kafka Client
### Description
A NestJS - KafkaJs Wrapper, wrapping on [KafkaJS](https://github.com/tulios/kafkajs)
### Installation
```bash
npm install nestjs-kafka
```### Add it to the NestJS app.module.ts or any module
```ts
import { KafkaModule } from 'nestjs-kafka';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 sendMessage
```ts
import {KafkaService, KafkaPayload} from "nestjs-kafka";@Injectable()
export class TaskKafkaProductService{
constructor(private readonly kafkaService: KafkaService) {}public async sendPushTask(kafkaTaskDto: KafkaTaskDto): Promise {
const message: KafkaTaskDto = kafkaTaskDto;
const payload: KafkaPayload = {
messageId: '' + new Date().valueOf(),
body: message,
messageType: TASK_PUSH_INFO,
topicName: TASK_PUSH_INFO,
};
this.kafkaService.sendMessage('test-kafka', payload);
}
}```
### How to Subscribe Message
```ts
import {KafkaPayload, AbstractKafkaConsumer} from "nestjs-kafka";@Injectable()
export class TaskKafkaConsumerService extends AbstractKafkaConsumer {constructor() {
super();
}
// register topic
protected registerTopic(): any {
this.addTopic('task.push.info');
this.addTopic('test-group');
}@SubscribeTo('task.push.info')
taskSubscriber(payload: string ): any {
const data: KafkaPayload = JSON.parse(payload);
}/**
* When application or container scale up &
* consumer group id is same for application
* @param payload
*/
@SubscribeToFixedGroup('test-group')
helloSubscriberToFixedGroup(payload: KafkaPayload): any {}
}
```