Skip to content

Instantly share code, notes, and snippets.

@AbhilashG97
Created May 27, 2021 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbhilashG97/3e5ecb6a4c53d6b65970e99daec3d05a to your computer and use it in GitHub Desktop.
Save AbhilashG97/3e5ecb6a4c53d6b65970e99daec3d05a to your computer and use it in GitHub Desktop.
Kafka JS consumer code
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