Node JS Kafka
Please go through my previous blog posts like setup Kafka in windows and how to write publisher/subscriber
write Kafka publisher/subscriber in node js
Get All the Topics in current Kafka listener
import kafkajs in your app.js file
const { Kafka } = require('kafkajs')
Now define kafka configuration like kafka listener/server. In my case it is 'YOURDESKTOP-TEST.mshome.net'
const kafka = new Kafka({
brokers: ['YOURDESKTOP-TEST.mshome.net:9092']
});
create kafka admin client using kafka.admin() like this:
const admin = kafka.admin()
use listTopics() method using admin like this:
const getAllTopics = async () => {
await admin.connect();
return await admin.listTopics();
};
getAllTopics() is a promise and we will have to resolve it.
return getAllTopics().then(allTopics => {
console.log(allTopics);
})
Get offsets of the topic
use fetchTopicOffsets() method using admin like this. We will have to pass topic name in this method
const fetchTopicOffsets = async (topicName) => {
await admin.connect();
return await admin.fetchTopicOffsets(topicName);
};
fetchTopicOffsets () is a promise and we will have to resolve it:
return fetchTopicOffsets(topicName.toString()).then(topicOffset => {
console.log(topicOffset);
})