3

There are several similar questions available on stackoverflow itself but still I am asking again because I want solution in NodeJS.

The scenario is -

(I am using kafkaJS library)

I have two topics topic-A and topic-B, so I subscribe to /topic-.*/, then topic-C is created, now I want my consumer to automatically subscribe to topic-c as well. But this is not happening.

I am getting only one solution that is, I have to stop my consumer and reinitiate it after every new topic creation.(But this approach seems tough to maintain when multiple topic will create dynamically).

Can anyone please suggest me any other solution with example. Thank you in advance.

P N Jain
  • 302
  • 1
  • 13

1 Answers1

2

Currently, it is not possible to subscribe to topic if your consumer is already running.

You can find the following issue about it in kafkaJS Github repository: Consumer: Fails to subscribe after run()

To quickly summarize this issue:

  • It is not possible to subscribe to a topic after consumer.run(). You will get the following error: Cannot subscribe to topic while consumer is running
  • The workaround (until it will be implemented some day) is to stop the consumer, fetch the list of topic from the consumer and append to it your new topic to be subscribed, and rerun the consumer.
Eliott Kespi
  • 116
  • 4