> Apache Kafka with Node.js

Event streaming with KafkaJS. Producers, consumers, consumer groups, and exactly-once semantics.

fetch
$curl "https://skillshub.wtf/skillshub-team/catalog-batch5/kafka-node?format=md"
SKILL.mdApache Kafka with Node.js

Kafka with KafkaJS

Setup

npm install kafkajs

Producer

const kafka = new Kafka({ clientId: 'app', brokers: ['localhost:9092'] });
const producer = kafka.producer();
await producer.connect();
await producer.send({ topic: 'events', messages: [{ key: 'user-1', value: JSON.stringify(event) }] });

Consumer

const consumer = kafka.consumer({ groupId: 'my-service' });
await consumer.subscribe({ topic: 'events' });
await consumer.run({
    eachMessage: async ({ topic, partition, message }) => {
        const event = JSON.parse(message.value!.toString());
        await processEvent(event);
    },
});

Admin

const admin = kafka.admin();
await admin.createTopics({ topics: [{ topic: 'events', numPartitions: 3 }] });

Key Patterns

  • Message keys for partition ordering
  • Consumer groups for parallel processing
  • Idempotent consumers (handle duplicates)
  • Transactional producer for exactly-once

┌ stats

installs/wk0
░░░░░░░░░░
first seenMar 18, 2026
└────────────

┌ tags

└────────────