Kafka with NestJS - send hangs

Viewed 725

The code is pretty similar to the official tutorial: https://docs.nestjs.com/microservices/kafka

kafka.module.ts:

import { Module } from '@nestjs/common'
import { ClientsModule, Transport } from '@nestjs/microservices'
import { KafkaClientController } from './kafka.controller'

@Module({
  imports: [
    ClientsModule.register([
      {
        name: 'KAFKA_SERVICE',
        transport: Transport.KAFKA,
        options: {
          client: {
            clientId: 'say',
            brokers: ['localhost:29092'], // docker container port
          },
          consumer: {
            groupId: 'say-consumer',
          },
        },
      },
    ]),
  ],
  controllers: [KafkaClientController],
})
export class KafkaModule {}

kafka.controller.ts:

import { Controller, Get, Inject, OnModuleDestroy, OnModuleInit } from '@nestjs/common'
import { ClientKafka } from '@nestjs/microservices'

@Controller('kafka')
export class KafkaClientController implements OnModuleInit, OnModuleDestroy {
  constructor(@Inject('KAFKA_SERVICE') private readonly kafka: ClientKafka) {}

  async onModuleInit() {
    this.kafka.subscribeToResponseOf('say.hello')
    await this.kafka.connect()
  }

  onModuleDestroy() {
    this.kafka.close()
  }

  @Get()
  async sayHello() {
    console.log('in sayHello')
    const x = await this.kafka.send('say.hello', 'hi this is the data').toPromise() // hangs here
    console.log(x)
  }
}

The method sayHello hangs, even if I replace the send line with:
return this.kafka.send('say.hello', 'hi this is the data')

I've seen this question for Scala: Kafka producer hangs on send

But I couldn't find an ack option: KafkaOptions KafkaConfig ProducerConfig ConsumerConfig

0 Answers
Related