I am implementing kafka adapter using NestJS adapters. Within this adapter, I need to be able to publish and subscribe to Kafka topics. The adapter class is not a controller, so I can't add @EventPattern() methods there. I can inject kafkaClient and be able to only send messages:
export class KafkaAdapter extends IoAdapter {
constructor(
private readonly app: INestApplicationContext,
private kafkaClient: ClientKafka,
) {
super(app);
}
createIOServer(port: number, options?: any): any {
const server: Server = super.createIOServer(port, options);
const client = this.kafkaClient;
server.adapter(function (nsp) {
// using kafkaClient to send/emit messages
});
return server;
}
}
But it seems I can't subscribe to Kafka topics with this kafkaClient. The consumer is created but not exposed to a public KafkaClient interface. So I forced to initialize another custom consumer, which is creating a separate connection to Kafka, and I don't like it.
Any way to subscribe to Kafka topics outside of controllers?