romulo-mesquita
romulo-mesquita

Reputation: 1

NestJs does not publish message in Kafka

I'm trying to publish a message to Kafka through a nestJs service. After running the code and checking the messages published in the topic, there is no message, and there is no error when executing the code that publishes the message.

main.ts

app.connectMicroservice<MicroserviceOptions>({
transport: Transport.KAFKA,
options: {
  client: {
    clientId: process.env.KAFKA_CLIENT_ID,
    brokers: process.env.KAFKA_BROKERS.split(';'),
  },
  consumer: {
    groupId: process.env.KAFKA_GROUP_ID,
  },
},
});

teste.module.ts

ClientsModule.registerAsync([
  {
    name: KAFKA_PRODUCER,
    inject: [ConfigService],
    useFactory: async (configService: ConfigService) => ({
      transport: Transport.KAFKA,
      options: {
        client: {
          clientId: configService.get('KAFKA_CLIENT_ID'),
          brokers: [configService.get('KAFKA_BROKER')],
        },
        consumer: {
          groupId: configService.get('KAFKA_GROUP_ID'),
        },
      },
    }),
  },
]),

teste.service.ts Constructor:

constructor(
@Inject(forwardRef(() => KAFKA_PRODUCER))
private readonly kafkaClient: ClientKafka,
){}

Function that publishes the message

async enviaDadoKafka(data: KafkaDado) {
try {
  this.kafkaClient.emit<void, KafkaDado>(
    `${process.env.KAFKA_NAME}.dado`,
    data,
  );
} catch (error) {
  console.log(error);
}
}

Since this application can listen to Kafka messages normally, message production is not happening, thus eliminating the issue of connecting to Kafka.

Could anyone help me, have any tips or ideas on what could be happening.

I tested the connection configuration with Kafka and the message listening part, both work normally, the publication is not happening.

Upvotes: 0

Views: 74

Answers (0)

Related Questions