Kafka with Node.js — Event Streaming Guide

Sanjeev SharmaSanjeev Sharma
2 min read

Advertisement

Kafka is Apache's distributed event streaming platform. Perfect for high-volume event processing and real-time analytics.

Setup

npm install kafkajs

Producer

import { Kafka } from "kafkajs";

const kafka = new Kafka({
  clientId: "my-app",
  brokers: ["localhost:9092"],
});

const producer = kafka.producer();
await producer.connect();

// Produce message
await producer.send({
  topic: "user-events",
  messages: [
    {
      key: "user-123",
      value: JSON.stringify({ type: "user-created", userId: 123 }),
    },
  ],
});

// Batch messages
await producer.send({
  topic: "user-events",
  messages: [
    { key: "user-1", value: JSON.stringify({ type: "user-created" }) },
    { key: "user-2", value: JSON.stringify({ type: "user-created" }) },
  ],
});

await producer.disconnect();

Consumer

const consumer = kafka.consumer({ groupId: "my-group" });
await consumer.connect();
await consumer.subscribe({ topic: "user-events" });

await consumer.run({
  eachMessage: async ({ topic, partition, message }) => {
    console.log({
      topic,
      partition,
      key: message.key?.toString(),
      value: message.value?.toString(),
    });

    const event = JSON.parse(message.value!.toString());
    // Process event
  },
});

Consumer Groups

// Multiple consumers same group = load balanced
// Different groups = all get same messages

const consumer1 = kafka.consumer({ groupId: "analytics" });
const consumer2 = kafka.consumer({ groupId: "notifications" });

// Both receive all messages independently

Partitions for Parallelism

// Produce with key (determines partition)
await producer.send({
  topic: "transactions",
  messages: [
    { key: "user-123", value: "..." }, // Always same partition
    { key: "user-456", value: "..." }, // Different partition
  ],
});

// Guarantees ordering within partition
// Allows parallel processing across partitions

FAQ

Q: Kafka vs RabbitMQ? A: Kafka for high-volume streaming, replay-able events. RabbitMQ for point-to-point messaging.

Q: How many partitions do I need? A: At least as many as max concurrent consumers. Start with 3-10.

Q: Can I replay events? A: Yes, Kafka stores messages. Read from any offset.


Kafka powers real-time data pipelines at companies like Netflix, Uber, LinkedIn.

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro