Created
July 11, 2024 07:16
-
-
Save un4ckn0wl3z/1bff0cbb77e680b4e25747218452d974 to your computer and use it in GitHub Desktop.
LibRdKafkaService.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from "@nestjs/common"; | |
import Kafka from 'node-rdkafka'; | |
import { ConfigService } from '@nestjs/config'; | |
@Injectable() | |
export class LibRdKafkaService { | |
private consumer: Kafka.KafkaConsumer; | |
private producer: Kafka.Producer; | |
constructor(private configService: ConfigService){ | |
this.consumer = new Kafka.KafkaConsumer({ | |
'metadata.broker.list': this.configService.get<string>('librdkafka-config.consumer.metadata-broker-list'), | |
'group.id': this.configService.get<string>('librdkafka-config.consumer.group-id') | |
}, {}) | |
this.producer = new Kafka.Producer({ | |
'metadata.broker.list': this.configService.get<string>('librdkafka-config.producer.metadata-broker-list'), | |
'client.id': this.configService.get<string>('librdkafka-config.producer.client-id') | |
}) | |
this.consumer.connect() | |
this.consumer | |
.on('ready', () => { | |
this.consumer.subscribe(['xxx']); | |
this.consumer.consume(); | |
}) | |
.on('data', (data) => { | |
console.log("[+] LibRdKafkaService", data.value.toString()); | |
}); | |
} | |
getConsumer() : Kafka.KafkaConsumer | |
{ | |
return this.consumer; | |
} | |
getProducer() : Kafka.Producer | |
{ | |
return this.producer; | |
} | |
} | |
// librdkafka-config: | |
// producer: | |
// client-id: "kafka" | |
// metadata-broker-list: "localhost:9092" | |
// consumer: | |
// group-id: "kafka", | |
// metadata-broker-list: "localhost:9092" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment