UNPKG

@yogaraj-saravanan/kafka-utils

Version:

Reusable Kafka producer and consumer utilities using kafkajs.

26 lines (25 loc) 752 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KafkaProducer = void 0; const kafkajs_1 = require("kafkajs"); class KafkaProducer { constructor(clientId, brokers) { this.clientId = clientId; this.brokers = brokers; const kafka = new kafkajs_1.Kafka({ clientId, brokers }); this.producer = kafka.producer(); } async connect() { await this.producer.connect(); } async send(topic, messages) { await this.producer.send({ topic, messages: messages.map((msg) => ({ value: JSON.stringify(msg) })), }); } async disconnect() { await this.producer.disconnect(); } } exports.KafkaProducer = KafkaProducer;