{"id":"283fc37085203da8","repo":"nestjs/nest","slug":"method-is-not-supported-for-kafka-client","errorCode":null,"errorMessage":"Method is not supported for Kafka client","messagePattern":"Method is not supported for Kafka client","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/microservices/client/client-kafka.ts","lineNumber":296,"sourceCode":"    } else {\n      throw new Error('No consumer initialized');\n    }\n  }\n\n  public unwrap<T>(): T {\n    if (!this.client) {\n      throw new Error(\n        'Not initialized. Please call the \"connect\" method first.',\n      );\n    }\n    return this.client as T;\n  }\n\n  public on<\n    EventKey extends string | number | symbol = string | number | symbol,\n    EventCallback = any,\n  >(event: EventKey, callback: EventCallback) {\n    throw new Error('Method is not supported for Kafka client');\n  }\n\n  protected registerConsumerEventListeners() {\n    if (!this._consumer) {\n      return;\n    }\n    this._consumer.on(this._consumer.events.CONNECT, () =>\n      this._status$.next(KafkaStatus.CONNECTED),\n    );\n    this._consumer.on(this._consumer.events.DISCONNECT, () =>\n      this._status$.next(KafkaStatus.DISCONNECTED),\n    );\n    this._consumer.on(this._consumer.events.REBALANCING, () =>\n      this._status$.next(KafkaStatus.REBALANCING),\n    );\n    this._consumer.on(this._consumer.events.STOP, () =>\n      this._status$.next(KafkaStatus.STOPPED),\n    );","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/nestjs/nest/blob/6ec0e2783d15290732447f304d8549b591b9749e/packages/microservices/client/client-kafka.ts#L278-L314","documentation":"Thrown by ClientKafka.on() because the Kafka client intentionally does not expose the generic event-listener API that the other transport clients (TCP, Redis, RMQ, MQTT) provide. Kafka event listening is handled internally via registerConsumerEventListeners()/registerProducerEventListeners() and exposed through the status Observable and a typed Kafka-specific on() override surface. Calling on() directly on a ClientKafka instance is an unsupported operation by design, not a connection problem.","triggerScenarios":"Calling client.on('connect', fn), client.on('disconnect', fn), or any client.on(event, callback) on an instance of ClientKafka (the object returned by ClientProxyFactory.create({ transport: Transport.KAFKA }) or ClientsModule). Any code ported from a Redis/TCP client that uses on() to listen for broker events will hit this on the Kafka transport.","commonSituations":"Migrating a microservice from Redis/RMQ/TCP transport to Kafka and copying the existing client.on(...) status wiring verbatim. Generic connection-monitoring code that loops over transports and calls on() uniformly. Using a shared base class that calls on() assuming all clients support it.","solutions":["Subscribe to the client.status Observable instead: client.status.subscribe(s => ...) emits KafkaStatus.CONNECTED/DISCONNECTED/REBALANCING/STOPPED/CRASHED.","If you need raw consumer/producer events, call client.unwrap() after connect to get the underlying KafkaJS consumer/producer and attach listeners there.","Remove any client.on(...) call against a ClientKafka instance; this path is unsupported and cannot be enabled via configuration."],"exampleFix":"// before\nconst client = ClientProxyFactory.create({ transport: Transport.KAFKA, options: {...} });\nclient.on('connect', () => console.log('connected'));\n\n// after\nclient.status.subscribe(status => {\n  if (status === KafkaStatus.CONNECTED) console.log('connected');\n});","handlingStrategy":"type-guard","validationCode":"import { ClientKafka } from '@nestjs/microservices';\n\nfunction supportsOn(client: any): boolean {\n  // ClientKafka.on() always throws 'Method is not supported for Kafka client'\n  return !(client instanceof ClientKafka);\n}\n\nif (supportsOn(client)) client.on('connect', fn);\nelse client.status.subscribe(s => { /* ... */ });","typeGuard":"import { ClientKafka } from '@nestjs/microservices';\nconst isKafkaClient = (c: unknown): c is ClientKafka => c instanceof ClientKafka;","tryCatchPattern":"if (client instanceof ClientKafka) {\n  client.status.subscribe(s => onStatus(s));\n} else {\n  client.on('connect', onConnect);\n}","preventionTips":["Treat ClientKafka event listening as status-Observable-only; never call on() on it.","When migrating transports, search the codebase for client.on( and replace with status subscriptions for Kafka.","Encapsulate transport-specific event wiring behind a factory rather than calling on() uniformly."],"tags":["kafka","client-proxy","events","api-misuse","typescript"],"analyzedSha":"6ec0e2783d15290732447f304d8549b591b9749e","analyzedAt":"2026-08-03T17:42:23.673Z","schemaVersion":2}