@confluentinc/kafka-javascript

中文文档
1.11.0发布于 2026/10/08MITTypeScriptCommonJS近 30 天下载 441.9万

Node.js bindings for librdkafka

原始使用文档
文档目录3 节
  1. Requirements
  2. Contributing
  3. Librdkafka Version

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:

  • High performance - confluent-kafka-javascript is a lightweight wrapper around librdkafka, a finely tuned C client.

  • Reliability - There are a lot of details to get right when writing an Apache Kafka client. We get them right in one place (librdkafka) and leverage this work across all of our clients.

  • Supported - Commercial support is offered by Confluent.

  • Future proof - Confluent, founded by the creators of Kafka, is building a streaming platform with Apache Kafka at its core. It's high priority for us that client features keep pace with core Apache Kafka and components of the Confluent Platform.

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:

  • Linux (x64 and arm64) - we support both musl and glibc based distributions:
DistributionSupported Node Versions
Debian Bullseye/Ubuntu 20.0418, 20, 21, 22, 23, 24
Debian Bookworm/Ubuntu 22.0418, 20, 21, 22, 23, 24
Alpine Linux 3.20+18, 20, 21, 22, 23, 24
AlmaLinux 9/Rocky Linux 9/CentOS Stream 918, 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.

  • Linux (s390x/IBM Z) - glibc and musl based distributions, Node versions 18, 20, 21, 22, 23 and 24. The pre-built binaries are tested on Ubuntu 20.04, 22.04, 24.04 and 26.04, Debian Trixie, Alpine Linux, and AlmaLinux 9/Rocky Linux 9/CentOS Stream 9.
  • macOS - arm64/m1. macOS (Intel) is supported on a best-effort basis. Node versions 18, 20, 21, 22, 23 and 24 are supported.
  • Windows - x64. Node versions 18, 20, 21, 22, 23 and 24 are supported.

[!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-javascriptlibrdkafka
1.0.02.6.1
1.2.02.8.0
1.3.02.10.0
1.3.12.10.0
1.3.22.10.1
1.4.02.11.0
1.4.12.11.1
1.5.02.11.1
1.6.02.12.0
1.7.02.12.1
1.8.02.13.0
1.8.22.13.2
1.9.02.14.0
1.9.12.14.2
1.10.02.15.0
1.10.12.15.1
1.11.02.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)'

版本与发布记录

@confluentinc/kafka-javascript 共发布 61 个版本,首次发布于 2024/02/16;过去 12 个月发布了 21 个版本。

版本发布日期距今
1.11.0latest2026/10/081 个月前
1.11.0-rc1rc2026/10/071 个月前
1.10.12026/09/102 个月前
1.10.1-rc12026/09/102 个月前
1.10.1-alpha.0alpha2026/08/272 个月前
1.10.02026/07/014 个月前

关于 @confluentinc/kafka-javascript 的常见问题

@confluentinc/kafka-javascript 是什么?

Node.js bindings for librdkafka

如何安装 @confluentinc/kafka-javascript?

在项目目录运行 npm install @confluentinc/kafka-javascript,或使用 pnpm add / yarn add / bun add。

@confluentinc/kafka-javascript 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@confluentinc/kafka-javascript 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@confluentinc/kafka-javascript 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@confluentinc/kafka-javascript 可以商用吗?

@confluentinc/kafka-javascript 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@confluentinc/kafka-javascript 的下载量有多少?

根据 npm 官方下载统计,@confluentinc/kafka-javascript 在 2026/10/11 – 2026/11/09 这 30 天内被下载 4,419,132 次,日均约 147,304 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@confluentinc/kafka-javascript 还在维护吗?

最新版本 1.11.0 发布于 2026/10/08(1 个月前);至今共发布 61 个版本,其中过去 12 个月发布了 21 个;首次发布于 2024/02/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。