Created
August 12, 2021 18:08
-
-
Save utkarsh867/c200bb0f8c7190ce302c4b9080c36fe8 to your computer and use it in GitHub Desktop.
2021_08_13 Kickstart with Kafka and Kafkajs
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: "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(); |
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: "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