1.0.2 • Published 3 years ago

common-kafkajs v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

About the Project

Common-KafkaJS is a modern Apache Kafka client for Node.js. It is compatible with Kafka 0.10+ and offers native support for 0.11 features. This library is combination of producer and consumer. You just need to install it and pass kafka configurations, topic name, message etc. No logic need to write.

Getting Started

npm install common-kafkajs

Usage

const { createProducer, createConsumer } = require("common-kafkajs");

// Producing
const produceSomething = async () => {
    try {
        const message = {
            "value": "some more msgsss",
            "partition": 0
        };

        await createProducer.kafkaProduce({
            "clientId": "myapp",
            "brokers": ["127.0.0.1:9092"],
            "topic": "Users",
            message
        });
        
    } catch (e) {
        console.error(`${e} Error while producing to Users topic.`)
    }
}

// Consuming
const consumeSomething = async () => {
    try {

        const consumer = await createConsumer.kafkaConsume({
            "clientId": "myapp",
            "brokers": ["127.0.0.1:9092"],
            "topic": "Users",
            "groupId": "test",
            "fromBeginning": true
        });

        await consumer.run({
            "eachMessage": async ({ topic, partition, message }) => {
                console.log(`Message ${message.value.toString()} consumed on partition ${partition}`);
            }
        })

        // await consumer.disconnect();
        
    } catch (e) {
        console.error(`${e} Error while consuming from Users topic`)
    }
}