Working with Kafka in TypeScript: KafkaJS Library and Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Kafka in TypeScript using KafkaJS



Apache Kafka is a distributed streaming platform widely used in microservice architecture, event processing, and real-time analytics. When developing in TypeScript, one of the most popular libraries for interacting with Kafka is KafkaJS. It provides a modern, asynchronous API with full TypeScript support, making it an ideal choice for building reliable and scalable applications.



What is KafkaJS?

KafkaJS is a client library for Apache Kafka written in JavaScript/TypeScript. It supports all major operations: sending and reading messages, managing consumer groups, working with topics and partitions. Thanks to built-in types, KafkaJS provides strict typing and autocompletion in the IDE, which significantly simplifies development and debugging.



Installing KafkaJS

To get started, install the library via npm:

npm install kafkajs

If you are using TypeScript, no additional types are required — they are already included in the package.



Basic Setup and Connection

The first step is to create a Kafka client instance. Specify the client ID and broker addresses:

import { Kafka } from 'kafkajs';

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

// Check connectionconst admin = kafka.admin();await admin.connect();console.log('Connected to Kafka');await admin.disconnect();


Creating a Producer

The producer sends messages to topics. KafkaJS supports automatic topic creation when sending (if enabled in the broker configuration).

import { Kafka, Partitioners } from 'kafkajs';

const producer = kafka.producer({ createPartitioner: Partitioners.DefaultPartitioner});

await producer.connect();

// Sending a single messageawait producer.send({ topic: 'orders', messages: [ { key: 'order-123', value: JSON.stringify({ userId: 1, amount: 250 }) } ]});

// Sending multiple messagesawait producer.send({ topic: 'orders', messages: [ { key: 'order-124', value: 'Message 2' }, { key: 'order-125', value: 'Message 3' } ]});

await producer.disconnect();


Creating a Consumer

The consumer reads messages from topics. It must subscribe to one or more topics and be registered in a consumer group.

const consumer = kafka.consumer({ groupId: 'order-group' });

await consumer.connect();await consumer.subscribe({ topic: 'orders', fromBeginning: true });

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


Working with Consumer Groups

Consumer groups allow distributing the load across multiple consumer instances. Kafka automatically assigns topic partitions to different consumers within the same group. If one consumer fails, its partitions are redistributed.

// Two consumers in the same group will read different partitionsconst consumer1 = kafka.consumer({ groupId: 'order-group' });const consumer2 = kafka.consumer({ groupId: 'order-group' });

// Each subscribes to the same topicawait consumer1.subscribe({ topic: 'orders' });await consumer2.subscribe({ topic: 'orders' });


Error Handling and Retries

In real-world applications, it is important to properly handle connection errors, timeouts, and failures. KafkaJS provides several mechanisms:

Recommendations