Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
2021_08_13 Kickstart with Kafka and Kafkajs
const { Kafka } = require("kafkajs");
const kafka = new Kafka({
clientId: "app",
brokers: ["localhost:9092"],
});
async function consumeMessages() {
const consumer = kafka.consumer({ groupId: "1" });
await consumer.connect();
await consumer.subscribe({
topic: "data",
fromBeginning: true,
});
await consumer.run({
eachMessage: ({ topic, partition, message }) => {
console.log(topic, partition, message, message.value.toString());
},
});
}
consumeMessages();
const { Kafka } = require("kafkajs");
const kafka = new Kafka({
clientId: "app",
brokers: ["localhost:9092"],
});
async function sendMessageOnProducer() {
const producer = kafka.producer();
await producer.connect();
await producer.send({
topic: "data",
messages: [
{
value: "hello",
},
],
});
producer.disconnect();
}
sendMessageOnProducer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment