@fastify/kafka

中文文档
4.0.1发布于 2026/10/08MITTypeScriptCommonJS构建来源已验证近 30 天下载 3836

Fastify plugin to interact with Apache Kafka.

原始使用文档
文档目录10 节
  1. Install
  2. Compatibility
  3. Usage
  4. Migration from node-rdkafka
  5. API
  6. Plugin options
  7. Producer
  8. Consumer
  9. Acknowledgments
  10. License

@fastify/kafka

CI NPM version neostandard javascript style

Fastify plugin to interact with Apache Kafka, supporting Kafka producers and consumers. To achieve the best performance, the plugin uses @platformatic/kafka.

Install

npm i @fastify/kafka

Compatibility

Plugin versionFastify version
>=3.x^5.x
>=0.x <3.x^4.x
>=0.x <3.x^3.x
>=0.x <3.x^2.x
>=0.x <3.x^1.x

Please note that if a Fastify version is out of support, then so are the corresponding versions of this plugin in the table above. See Fastify's LTS policy for more details.

Usage

const crypto = require('node:crypto')
const fastify = require('fastify')()
const group = crypto.randomBytes(20).toString('hex')

fastify
  .register(require('@fastify/kafka'), {
    producer: {
      bootstrapBrokers: ['127.0.0.1:9092'],
      clientId: 'my-producer',
      allowAutoTopicCreation: true
    },
    consumer: {
      bootstrapBrokers: ['127.0.0.1:9092'],
      groupId: group,
      clientId: 'my-consumer',
      allowAutoTopicCreation: true
    }
  })

fastify.post('/data', (req, reply) => {
  fastify.kafka.push({
    topic: 'updates',
    payload: req.body,
    key: 'dataKey'
  })
  reply.send({ ok: true })
})

fastify.kafka.subscribe('updates')
fastify.kafka.on('updates', (msg, commit) => {
  console.log(msg.value.toString())
  commit()
})

fastify.listen({ port: 3000 }, err => {
  if (err) throw err
  console.log(`Server listening on ${fastify.server.address().port}`)
  fastify.kafka.consume()
})

For more examples on how to use this plugin, you can take a look at the examples directory.

Migration from node-rdkafka

Starting from version 3.x, this plugin uses @platformatic/kafka instead of node-rdkafka.

The main differences in the configuration options are:

node-rdkafka (old)@platformatic/kafka (new)
metadata.broker.listbootstrapBrokers: ['host:port']
group.idgroupId
fetch.wait.max.msmaxWaitTime
enable.auto.commitautocommit
auto.offset.resetpassed via consumerTopicConf
dr_cbnot needed

API

This module exposes the following APIs:

Plugin options

{
  producer?: Record<string, unknown>   // @platformatic/kafka Producer options
  consumer?: Record<string, unknown>   // @platformatic/kafka Consumer options
  producerTopicConf?: Record<string, unknown>
  consumerTopicConf?: Record<string, unknown>
  metadataOptions?: Record<string, unknown>
}

Producer

  • fastify.kafka.producer — the producer instance
  • fastify.kafka.push(message) — send a message to a topic
fastify.kafka.push({
  topic: 'my-topic',   // required
  payload: 'hello',    // required - string or Buffer
  key: 'myKey',        // optional
  partition: 0         // optional
})

Consumer

  • fastify.kafka.consumer — the consumer instance
  • fastify.kafka.subscribe(topic | topic[]) — subscribe to one or more topics
  • fastify.kafka.consume() — start streaming messages (flow mode)
  • fastify.kafka.consume(n, callback) — fetch exactly n messages (batch mode)
  • fastify.kafka.on(topic, handler) — listen for messages on a specific topic
// Flow mode
fastify.kafka.subscribe(['topic-a', 'topic-b'])
fastify.kafka.on('topic-a', (msg, commit) => {
  console.log(msg.value.toString())
  commit() // manually commit the offset
})
fastify.kafka.consume()

// Batch mode
fastify.kafka.subscribe('topic-a')
fastify.kafka.consume(10, (err, messages) => {
  if (err) throw err
  messages.forEach(msg => console.log(msg.value.toString()))
})

Acknowledgments

This project is kindly sponsored by:

Past sponsors:

  • LetzDoIt

License

Licensed under MIT.

版本与发布记录

@fastify/kafka 共发布 12 个版本,首次发布于 2022/04/27;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.0.1latest2026/10/081 个月前
4.0.02026/07/024 个月前
3.0.22025/01/061.9 年前
3.0.12024/11/022 年前
3.0.0next2024/09/072 年前
3.0.0-pre.fv5.22024/08/082 年前

关于 @fastify/kafka 的常见问题

@fastify/kafka 是什么?

Fastify plugin to interact with Apache Kafka.

如何安装 @fastify/kafka?

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

@fastify/kafka 支持 TypeScript 吗?

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

@fastify/kafka 支持 ESM 还是 CommonJS?

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

@fastify/kafka 可以商用吗?

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

@fastify/kafka 的下载量有多少?

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

@fastify/kafka 还在维护吗?

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