Confluent's JavaScript Client for Apache Kafka®

confluent-kafka-javascript is Confluent's JavaScript client for Apache Kafka and the Confluent Platform. The goal is to provide an highly performant, reliable and easy to use JavaScript client that is based on node-rdkafka yet also API compatible with KafkaJS to provide flexibility to users and streamline migrations from other clients.

Features:

This library leverages the work and concepts from two popular Apache Kafka JavaScript clients: node-rdkafka and KafkaJS. The core is heavily based on the node-rdkafka library, which uses our own librdkafka library for core client functionality. However, we leverage a promisified API and a more idiomatic interface, similar to the one in KafkaJS, making it easy for developers to migrate and adopt this client depending on the patterns and interface they prefer. We're very happy to have been able to leverage the excellent work of the many authors of these libraries!

To use Schema Registry, use the @confluentinc/schemaregistry library that is compatible with this library. For a simple schema registry example, see sr.js.

Requirements

The following configurations are supported:

Distribution Supported Node Versions
Debian Bullseye/Ubuntu 20.04 18, 20, 21, 22, 23, 24
Debian Bookworm/Ubuntu 22.04 18, 20, 21, 22, 23, 24
Alpine Linux 3.20+ 18, 20, 21, 22, 23, 24
AlmaLinux 9/Rocky Linux 9/CentOS Stream 9 18, 20, 21, 22, 23, 24

Other distributions will probably work given a modern version of gcc/glibc, but we don't test the pre-built binaries with them.

[!WARNING] Pre-built binary support will be dropped after the EOL of the node version or the OS.

Installation on any of these platforms is meant to be seamless, without any C/C++ compilation required.

npm install @confluentinc/kafka-javascript

In case your system configuration is not within the supported ones, check the detailed installation instructions for more information.

Yarn and pnpm support is experimental.

Getting Started

Below is a simple produce example using the promisified API.

Production applications should serialize with Schema Registry. Producing plain string values leads to data-quality issues, broken consumers, and ungovernable data. This example uses the companion @confluentinc/schemaregistry package, which is installed separately.

Replace each <fill> with your own values. The example talks to two endpoints — a Kafka cluster and a Schema Registry — and on Confluent Cloud each has its own API key and secret.

const { Kafka } = require('@confluentinc/kafka-javascript').KafkaJS;
const { kafkaJsonSerializerBuilder } = require('@confluentinc/schemaregistry');

async function producerStart() {
    // The producer builds the serializer while it connects and applies it to
    // every message value. autoRegisterSchemas registers the schema on first
    // produce. Use useLatestVersion instead when the schema is already registered.
    const valueSerializerBuilder = kafkaJsonSerializerBuilder()
        .setClientConfig({
            baseURLs: ['<fill>'],
            basicAuthCredentials: {
                credentialsSource: 'USER_INFO',
                // Schema Registry API key and secret, as '<key>:<secret>'. These
                // are a separate credential from the Kafka API key used below.
                userInfo: '<fill>:<fill>',
            },
        })
        .setJsonSerializerConfig({ autoRegisterSchemas: true });

    const producer = new Kafka().producer({
        'bootstrap.servers': '<fill>',
        'security.protocol': 'SASL_SSL',
        'sasl.mechanisms': 'PLAIN',
        'sasl.username': '<fill>',
        'sasl.password': '<fill>',
        'js.value.serializer.builder': valueSerializerBuilder,
    });

    await producer.connect();
    console.log("Connected successfully");

    const user = { name: 'Confluent', favoriteNumber: 42 };
    await producer.send({
        topic: 'test-topic',
        messages: [
            { value: user, key: 'user1' },
        ],
    });

    await producer.disconnect();
    console.log("Disconnected successfully");
}

producerStart();

The Schema Registry serde integration (the js.*.serializer.builder and js.*.deserializer.builder properties) and clusterId() are experimental and may change in future releases.

For an Avro example that also consumes and deserializes, see sr.js.

There are two variants of the API offered by this library. A promisified API and a callback-based API.

  1. If you're starting afresh, you should use the promisified API.
  2. If you're migrating from kafkajs, you can use the migration guide to get started quickly.
  3. If you're migrating from node-rdkafka, you can use the migration guide.

An in-depth reference may be found at INTRODUCTION.md.

Contributing

Bug reports and feedback is appreciated in the form of Github Issues. For guidelines on contributing please see CONTRIBUTING.md

Librdkafka Version

confluent-kafka-javascript librdkafka
1.0.0 2.6.1
1.2.0 2.8.0
1.3.0 2.10.0
1.3.1 2.10.0
1.3.2 2.10.1
1.4.0 2.11.0
1.4.1 2.11.1
1.5.0 2.11.1
1.6.0 2.12.0
1.7.0 2.12.1
1.8.0 2.13.0
1.8.2 2.13.2
1.9.0 2.14.0
1.9.1 2.14.2
1.10.0 2.15.0
1.10.1 2.15.1
1.11.0-rc1 2.16.0

This mapping is applicable if you're using a pre-built binary. Otherwise, you can check the librdkafka version with the following command:

node -e 'console.log(require("@confluentinc/kafka-javascript").librdkafkaVersion)'