Search code examples
javascripttypescriptapache-kafkakafkajs

Can't send Kafka messages though topic and producer both exist


I'm working in TypeScript with the KafkaJS library locally, with a single kafka broker. I've connected a producer successfully, have verified that my topic was created, and am generating messages with:


  const changeMessage = {
    key: id,
    value: JSON.stringify(person),
    headers: {
      changeType: status,
    },
  };

Now when I go to send the message:


  try {
    const sendResponse = await producer.send({
      topic: topicName2,
      messages: [changeMessage],
    });
    log.responseFragment(
      { id, topicName2 },
      `Sending changed/added person ${id} to topic ${topicName2}`
    );
  } catch (error) {
    log.error(
    { error }, `Could not send personChangedAdded ${id} to topic ${topicName2}`
    );
  }

Here's the error that I get back:

Could not send personChange to topic topicName2
    error: {
      "name": "KafkaJSError",
      "retriable": true
    }

Solution

  • I had failed to define log.responseFragment(), and when I changed it to a simple log.info() the problem was resolved.