0

i have this producer

const { Kafka } = require("kafkajs");

const kafka = new Kafka({
  clientId: "app",
  brokers: ["localhost:29092"],
});

async function sendMessageOnProducer() {
  const producer = kafka.producer();
  await producer.connect();
  await producer.send({
    topic: "test",
    messages: [
      {
        value: "hello",
      },
    ],
  });
  producer.disconnect();
}

sendMessageOnProducer();

and i have this docker compose

version: '2'
services:
  zookeeper:
    container_name: cluster
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
  kafka:
    container_name: kafka
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper
    ports:
      - 29092:29092
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:29092
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
  

but when i run my producer i have this error

{"level":"ERROR","timestamp":"2023-05-30T14:58:26.187Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to seed broker, trying another broker from the list: Connection error: connect ECONNREFUSED ::1:29092","retryCount":5,"retryTime":6966}

i'm trying to send a payload to my kafka

  • Is your node code running in its own container? – OneCricketeer May 30 '23 at 15:28
  • You're missing a `KAFKA_LISTENERS` variable – OneCricketeer May 30 '23 at 15:29
  • i added the KAFKA_LISTENERS variable but i have the same error @OneCricketeer – KHALEF Ahmed May 30 '23 at 16:22
  • no i run it with npm start – KHALEF Ahmed May 30 '23 at 16:23
  • What did you set it to? It needs to be `KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:29092`; Also, 1) there is no `test` topic being created anywhere 2) The Kafka Container is not immediately available. You should add a sleep in your JS code or the npm script – OneCricketeer May 30 '23 at 18:49
  • yes i added it like that and i created my topic when i run : docker exec kafka kafka-topics --bootstrap-server localhost:29092 --list , i see the topic name that i added @OneCricketeer – KHALEF Ahmed May 30 '23 at 22:35
  • Okay. Great. As mentioned, container doesn't start immediately. Try adding sleep before connecting from KafkaJS. – OneCricketeer May 31 '23 at 02:55
  • i add this and i can't connect to my kafka : await new Promise((resolve) => setTimeout(resolve, 5000)); @OneCricketeer – KHALEF Ahmed May 31 '23 at 08:36
  • Are you sure Kafka container actually started? Your Zookeeper container is called cluster, but you never use that. See working file, which is also linked in the duplicate post https://github.com/conduktor/kafka-stack-docker-compose/blob/master/zk-single-kafka-single.yml#L7 – OneCricketeer May 31 '23 at 20:50

0 Answers0