kafkajs-common-npm
v1.2.5
Published
A Kafka package for automatic configuration
Downloads
54
Readme
Usage of this Npm LIKE THIS : import { initializeKafka, KafkaConfig } from 'my-kafka-package';
const config: KafkaConfig = { clientId: 'my-client-id', brokers: ['localhost:9092'], topics: { order: 'order-topic', cart: 'cart-topic', wishlist: 'wishlist-topic' } };
const processMessage = async (topic: string, partition: number, message: any): Promise => { console.log('Processing message', { topic, partition, offset: message.offset, value: message?.value?.toString(), }); // Add your custom message processing logic here };
initializeKafka(config, processMessage).then(() => { console.log('Kafka initialized'); }).catch(err => { console.error('Error initializing Kafka:', err); });
Create Topics
import { createTopics } from 'my-kafka-package';
const topics = [ { topic: 'order-topic', numPartitions: 2, replicationFactor: 3, configEntries: [{ name: 'retention.ms', value: '86400000' }] }, { topic: 'cart-topic', numPartitions: 2, replicationFactor: 3, configEntries: [{ name: 'retention.ms', value: '86400000' }] } ];
createTopics(topics).then(() => { console.log('Topics created'); }).catch(err => { console.error('Error creating topics:', err); });
Delete Topics
import { deleteTopics } from 'my-kafka-package';
const topicsToDelete = ['order-topic', 'cart-topic'];
deleteTopics(topicsToDelete).then(() => { console.log('Topics deleted'); }).catch(err => { console.error('Error deleting topics:', err); });
Increase Partition
import { increasePartitions } from 'my-kafka-package';
increasePartitions('order-topic', 5).then(() => { console.log('Partitions increased'); }).catch(err => { console.error('Error increasing partitions:', err); });