Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to connect kafka in next js client side #1717

Open
Adil7767 opened this issue Oct 24, 2024 · 0 comments
Open

unable to connect kafka in next js client side #1717

Adil7767 opened this issue Oct 24, 2024 · 0 comments

Comments

@Adil7767
Copy link

Adil7767 commented Oct 24, 2024

``Describe the bug
i have tried to send a message on Kafka but unable to connect Kafka
To Reproduce
Please provide either a link to a:

my code look like

`import { Kafka } from 'kafkajs';
import ip from 'ip';

// Get the host IP, defaulting to localhost
const host = process.env.HOST_IP || ip.address();

// Function to create and connect Kafka producer
const createAndConnectProducer = async () => {
  const kafka = new Kafka({
    clientId: 'my-app',
    brokers: [`${host}:9092`], // Specify Kafka broker address
  });

  const producer = kafka.producer();

  try {
    await producer.connect();
    console.log('Kafka producer connected successfully!');
  } catch (error) {
    console.error('Failed to connect to Kafka:', error);
  }

  console.log(producer , 'producer.event.CONNECTED')
  return producer;
};
// Function to send a message to Kafka
const sendKafkaMessage = async (producer, topic, message) => {
  try {
  console.log(producer, topic, message,'producer, topic, message)')
    await producer.send({
      topic,
        messages: [
        { value: 'Hello Kafka' }
      ]
    });

    console.log('Message sent successfully:', message);
  } catch (error) {
    console.error('Failed to send message to Kafka:', error);
  }
};

// Function to produce a message dynamically
const produceKafkaMessages = async (topic, message) => {
  const producer = await createAndConnectProducer();
  await sendKafkaMessage(producer, topic, message);
  await producer.disconnect(); 
};


export default produceKafkaMessages;
// To use the producer:
// produceKafkaMessages('my-topic', 'my-message');
`

but i always got error

`{"level":"ERROR","timestamp":"2024-10-24T19:30:35.110Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to seed broker, trying another broker from the list: Failed to connect: net.connect is not a function","retryCount":4,"retryTime":6874}`
and 
`kafka-producer.js:20 Failed to connect to Kafka: KafkaJSNumberOfRetriesExceeded: Failed to connect: net.connect is not a function`

and

Failed to send message to Kafka: KafkaJSError: The producer is disconnected
    at validateConnectionStatus (messageProducer.js:31:15)
    at sendBatch (messageProducer.js:82:5)
    at Object.send (messageProducer.js:120:12)
    at sendKafkaMessage (kafka-producer.js:30:20)
    at produceKafkaMessages (kafka-producer.js:46:9
@Adil7767 Adil7767 changed the title un able to connect kafka in next js client side unable to connect kafka in next js client side Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant