0

This is my producer using kafkajs. When I provide incorrect user name in options.username, I see the message Connected. How do I handle connection errors with kafkajs?

const { Kafka, logLevel } = require('kafkajs')

async function kafkaProducer(options) {
    const kafka = new Kafka({
        brokers: [options.bootstrapServer],
        clientId: options.clientId,
        ssl: {
            rejectUnauthorized: false,
            ca: [fs.readFileSync(options.caCertPath, 'utf-8')]
        },
        sasl: {
            mechanism: options.saslMechanism, // PLAIN
            username: options.username,
            password: options.password
        },
        requestTimeout: 2000,
        retry: {
            retries: 1
        },
        logLevel: logLevel.ERROR
    })
    //
    const producer = kafka.producer()
    try {
        await producer.connect()
        console.log('Connected')
    } catch(e) {
        throw new Error(e)
    }
}
cogitoergosum
  • 2,309
  • 4
  • 38
  • 62

1 Answers1

1

KafkaJS will throw an error on connect if authentication fails. See this test for example.

It is very likely that you haven't set up authentication on the broker side correctly. See the documentation for information on how to configure SASL on the broker.

Tommy Brunn
  • 2,520
  • 2
  • 29
  • 41