Last active
February 13, 2020 23:42
-
-
Save eyeezzi/dbe505efa05c09ed8c17c5c6e51c091c to your computer and use it in GitHub Desktop.
Connecting producer to Aiven Kafka not working
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ensure process.env contains all the needed environment variables. | |
const kafka = require('node-rdkafka') | |
console.log(JSON.stringify(kafka.features)) | |
// ["gzip","snappy","ssl","sasl","regex","lz4","sasl_gssapi","sasl_plain","sasl_scram","plugins","zstd","sasl_oauthbearer"] | |
const createProducer = (onDeliveryReport) => { | |
const producer = new kafka.Producer({ | |
'metadata.broker.list': process.env.KAFKA_BROKERS_LIST, | |
'sasl.username': process.env.KAFKA_CLIENT_USERNAME, | |
'sasl.password': process.env.KAFKA_CLIENT_PASSWORD, | |
'security.protocol': 'SASL_SSL', | |
'sasl.mechanisms': 'PLAIN', | |
'dr_msg_cb': true | |
}) | |
return new Promise((resolve, reject) => { | |
producer | |
.on('ready', () => { resolve(producer) }) | |
.on('event.error', (err) => { reject(err) }) | |
.on('delivery-report', onDeliveryReport) | |
producer.connect() // <-- THIS IS WHERE EXECUTION HANGS | |
}) | |
} | |
async function produceMessage(msg) { | |
producer = await createProducer((err, report) => { | |
console.log(`DELIVERY_REPORT: Error ${err}`) | |
console.log(`DELIVERY_REPORT: Report ${report}`) | |
}) | |
// THE LINES BELOW NEVER EXECUTE. | |
producer.produce(process.env.PRODUCE_TOPIC, -1, Buffer.from(JSON.stringify(msg))) | |
console.log(`produced ${msg}`) | |
} | |
produceMessage("hello kafka") | |
// NOTE: The code execution goes like this (line-by-line): 39 -> 28 -> 8 -> 18 -> 23 [HANGS] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment