How to write an array(has json strings) to Kakfa topic in node js?
How to write an array(has json strings) to Kakfa topic in node js?
server.on('package:publish', async event => {
try {
const responses = await producer.send({
topic: process.env.TOPIC,
messages: [{
// Name of the published package as key, to make sure that we process events in order
key: event.name,
// The message value is just bytes to Kafka, so we need to serialize our JavaScript
// object to a JSON string. Other serialization methods like Avro are available.
value: JSON.stringify(array)
}]
})
console.log('Published message', { responses })
} catch (error) {
console.error('Error publishing message', error)
}
})
The message value is just bytes to Kafka, so we need to serialize our JavaScript object to a JSON string. Other serialization methods like Avro are available.
Just wrap your array data as JSON.stringify(array) and send it in value,
and at consumer end you can parse it using JSON.parse(message.value)
Here are some links for help https://www.confluent.io/blog/getting-started-with-kafkajs/