Skip to main content

KafkaJS Client

The KafkaJS client can be used to create Kafka producers, consumers. Full documentation here.


Install KafkaJS using:

yarn add kafkajs

or npm:

npm install kafkajs

Instantiate the KafkaJS client by pointing it towards at least one broker:

const { Kafka } = require('kafkajs')

const kafka = new Kafka({
clientId: '<my-app>',
brokers: ['<my-gdn>.<my-paas>'],
sasl: { mechanism: 'plain', username: "<my-tenant>/<my-fabric>", password: "token:<my-api-key>" },
ssl: {
rejectUnauthorized: false


To produce a message to a topic, create a producer using our client:

const producer = kafka.producer()

await producer.connect()
await producer.send({
topic: '<c8globals or c8locals>.<my-topic>',
messages: [
{ value: 'Hello KafkaJS user!' },

await producer.disconnect()


To verify the message has been sent to the topic, create a consumer to consume the message:

const consumer = kafka.consumer({ groupId: '<my-group-id>' })

await consumer.connect()
await consumer.subscribe({ topic: '<c8globals or c8locals>.<my-topic>', fromBeginning: true })

eachMessage: async ({ topic, partition, message }) => {
value: message.value.toString(),