Kafka Javascript Example Tutorial With Code Snippets

Bobby Jul 13, 2026

Apache Kafka is a robust, open-source distributed event streaming platform. It's widely used in distributed systems for building real-time data pipelines and streaming apps. If you're a JavaScript developer tinkering with Kafka, you're in the right place. Let's explore a hands-on example of producing and consuming messages with Kafka using Node.js.

Alex Xu (@alexxubyte) on X
Alex Xu (@alexxubyte) on X

Before diving into the code, ensure you have Kafka and Zookeeper installed and running locally or use Docker for a quick setup. Also, install the `kafkajs` library, which provides an easy-to-use client for Kafka in JavaScript: `npm install kafkajs`.

kafka
kafka

Producers in Kafka with JavaScript

Producers push data into Kafka topics. Let's create a simple Node.js application sending a message to the 'my-topic' topic.

the diagram shows how kafka works and what it is like to work with them
the diagram shows how kafka works and what it is like to work with them

First, import the required modules and set up an admin client to interact with Kafka:

```javascript const Kafka = require('kafkajs'); const kafka = new Kafka.Kafka({ clientId: 'my-app', brokers: ['localhost:9092'], }); const admin = kafka.admin(); ```

Creating a Kafka Topic

a diagram showing the architecture of a kafka architecture, with different types of products and
a diagram showing the architecture of a kafka architecture, with different types of products and

Before sending messages, create a topic:

```javascript await admin.createTopics({ topics: ['my-topic'], waitForAllTopicsToBeCreated: true, }); ```

Sending Messages

Now, produce a message using the `Kafka.Producer` class:

the kafka at user diagram
the kafka at user diagram

```javascript const producer = kafka.producer(); producer.connect(); await producer.send({ topic: 'my-topic', messages: [ { value: 'This is my first Kafka message!' }, ], }); producer.disconnect(); ```

Consumers in Kafka with JavaScript

Consumers fetch data from Kafka topics. Let's build a consumer that reads messages from the 'my-topic' topic we created.

First, set up a consumer client:

the kafka on kubernets diagram shows how it is used to store products
the kafka on kubernets diagram shows how it is used to store products

```javascript const consumer = kafka.consumer({ groupId: 'my-group' }); ```

Consuming Messages

Now, consume messages from the 'my-topic' topic:

the diagram shows how to use kafka for data processing and storage in an application
the diagram shows how to use kafka for data processing and storage in an application
the top 5 kafka use cases
the top 5 kafka use cases
Omkar Srivastava on LinkedIn: #devops #engineering #softwareengineer #kubernetes | 66 comments
Omkar Srivastava on LinkedIn: #devops #engineering #softwareengineer #kubernetes | 66 comments
Kafka intro
Kafka intro
the top 8 kafka use cases
the top 8 kafka use cases
Top 5 Kafka Use Cases
Top 5 Kafka Use Cases
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
the top 6 kaka use cases are shown in this graphic above it is an image of
the top 6 kaka use cases are shown in this graphic above it is an image of
the diagram shows how kafka is used to create an application for data storage
the diagram shows how kafka is used to create an application for data storage
a diagram showing the different types of cloud computing
a diagram showing the different types of cloud computing
JavaScript Basics | Chapter 1 📖 | Part 1
JavaScript Basics | Chapter 1 📖 | Part 1
Apache Kafka Infographic
Apache Kafka Infographic
Client Challenge
Client Challenge
→ Top 6 Kafka Use Cases
→ Top 6 Kafka Use Cases
Apache Kafka For Data Pipeline
Apache Kafka For Data Pipeline
Rabbit MQ vs Kafka
Rabbit MQ vs Kafka
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡
a diagram showing how to use the kaka fast appliance for an application
a diagram showing how to use the kaka fast appliance for an application
The Power of Kafka: Top 5 Use Cases Revealed
The Power of Kafka: Top 5 Use Cases Revealed
JavaScript Basics You MUST Know
JavaScript Basics You MUST Know

```javascript consumer.subscribe({ topic: 'my-topic', fromBeginning: true }); consumer.run({ eachMessage: async ({ topic, partition, message }) => { console.log({ value: message.value.toString() }); }}); ```

Handling Commitment

Consumers automatically commit offsets to Kafka for their group regularly. However, you might want to handle this manually in some scenarios:

```javascript consumer.run({ eachMessage: async ({ topic, partition, message }) => { await consumer.commit({ topic, partition, offsets: { [partition]: message.offset }, }); }}); ```

There you have it! A basic example of producing and consuming messages using Kafka with Node.js. Dive deeper into Kafka's features, such as using multiple brokers, handling failures, or processing streams with Kafka Streams API, to build robust, distributed data pipelines.