'nodejs fails to connect to kafka broker

I'm trying to connect to a Kafka 3.0.0 client running on ubuntu for windows but I get the following error: "message":"[Connection] Connection error: connect ECONNREFUSED 127.0.0.1:9092".

This supposedly means that node can't find any service running on port 9092 but when I check all active tcp ports it says that kafka is listening on 9092:

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp6       0      0 :::9092                 :::*                    LISTEN
tcp6       0      0 :::2181                 :::*                    LISTEN
tcp6       0      0 127.0.0.1:46550         127.0.1.1:9092          ESTABLISHED
tcp6       0      0 127.0.1.1:9092          127.0.0.1:46550         ESTABLISHED
tcp6       0      0 127.0.0.1:2181          127.0.0.1:54466         ESTABLISHED
tcp6       0      0 127.0.0.1:54466         127.0.0.1:2181          ESTABLISHED

I have no idea why this is happening, all server.properties settings are default, zookeeper is running, and I am able to create topics but it node still fails to connect.

--Edit -- This is my serverside code:

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

    const run = async () => {
        try {
            const kafka = new Kafka({
                clientId: "myapp",
                brokers: ["127.0.0.1:9092"]
            })  
            const name = process.argv[2];
            const producer = kafka.producer();
            await producer.connect();
            console.log("connected")
    
            await producer.send({
                topics: "hashtags",
                messages: [
                    {
                        value: name
                    }
                ]
            })
            console.log("done")
            await producer.disconnect();
        } catch (error) {
            console.log(error)
        } 
    }
    
run();

This is the relevant content of the server.config file:

I updated listeners=PLAINTEXT://:9092 to listeners=PLAINTEXT://127.0.0.1:9092

#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
listeners=PLAINTEXT://127.0.0.1:9092

Now, after restarting zookeeper and kafka, the active tcp ports are:

tcp6       0      0 :::45815                :::*                    LISTEN
tcp6       0      0 127.0.0.1:9092          :::*                    LISTEN
tcp6       0      0 :::2181                 :::*                    LISTEN
tcp6       0      0 :::44587                :::*                    LISTEN
tcp6       0      0 127.0.0.1:46734         127.0.0.1:9092          ESTABLISHED
tcp6       0      0 127.0.0.1:9092          127.0.0.1:46734         ESTABLISHED
tcp6       0      0 127.0.0.1:54492         127.0.0.1:2181          ESTABLISHED
tcp6       0      0 127.0.0.1:2181          127.0.0.1:54492         ESTABLISHED

The error still persists.

Any help will be greatly appriciated!



Solution 1:[1]

I experienced this issue as well on Mac.

Context: I have set up two docker images, one for the zookeeper and one for Kafka.

I fixed it by using my actual IPv4 address.

Updated my brokers from ["127.0.0.1:9092"] and now I have something like ["192.168.2.2:9092"].

Solution 2:[2]

If you're using VS Code and/or have just updated Node.js, kill all terminals and create new ones then retry.

Solution 3:[3]

Try to reinstall Npm, By using npm install

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ouflak
Solution 2 Frank
Solution 3 Nishith