Created
May 27, 2021 03:57
-
-
Save AbhilashG97/3e5ecb6a4c53d6b65970e99daec3d05a to your computer and use it in GitHub Desktop.
Kafka JS consumer code
This file contains 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
const { Kafka } = require("kafkajs"); | |
const kafka = new Kafka({ | |
clientId: "kafka-tweets", | |
brokers: ["localhost:9092"], | |
}); | |
const consumer = kafka.consumer({ groupId: "tweets_cg_one" }); | |
const runConsumer = async () => { | |
await consumer.subscribe({ | |
topic: "tweets_node", | |
fromBeginning: true, | |
}); | |
await consumer.run({ | |
eachMessage: async ({ topic, partition, message }) => { | |
console.log({ | |
key: message.key.toString(), | |
value: message.value.toString(), | |
headers: message.headers, | |
}); | |
}, | |
}); | |
}; | |
// run the consumer | |
runConsumer(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment