Multiplatform JS with Apache Kafka not running

I made the example from Using packages from npm and that worked. So I want to try another one. I want to interact with Apache Kafka with the KafkaJS package.

When I try to create a consumer in this way:

fun createConsumer(): dynamic {
return js(
“kafka.consumer({groupID: ‘test-group’ })”

And call them from NodeJS I get the following error:
ReferenceError: kafka is not defined

The KafkaJS initially requires to create a Kafka instance, and only after that you can call a producer and consumer on instance.
unfortunately KafkaJS uses @types/nodejs, which can`t generated by dukat, but there is a small trick how to solve that.

  1. Add KotlinX NodeJS as dependency

  2. Run task nodeJsGenerateExternals and find the declarations in the externals folder

  3. Copy the generated files inside your sources

  4. Add to index.module_kafkajs.kt on top @file:JsModule(“kafkajs”)

  5. Move all typealiases (non external) to other file (e.g. index.module.kafkajs2.kt)

And now them implementation is straight forward.

    class KafkaTableTopicConfig (var kafkaConfig: KafkaConfig) {
    var kafkaTopic: String = ""

    fun createKConsumer() : dynamic {
        return KafkaFactory().createKafkaConsumer(this@KafkaTableTopicConfig)

    fun createKProducer() : dynamic {
        return KafkaFactory().createKProducer(this@KafkaTableTopicConfig)

And now the Factory:
class KafkaFactory {

    fun createKafkaConsumer(tableTopicConfig: KafkaTableTopicConfig): dynamic {
        return KafkaConsumer(tableTopicConfig).createConsumer()

    fun createKafkaProducer(tableTopicConfig: KafkaTableTopicConfig): dynamic {
        return KafkaProducer(tableTopicConfig).createProducer()

and then the producer:
class KafkaProducer (private val tableTopicConfig: KafkaTableTopicConfig) {

    private var producer: dynamic = ""

    fun createProducer(): dynamic {
        val kafkaOptions: KafkaConfig = js("({})")
        kafkaOptions.brokers = arrayOf(tableTopicConfig.kafkaConfig.bootstrapServer)
        kafkaOptions.clientId = "7Facette_Producer_" + (0..36).shuffled().first().toString()
        producer = Kafka(kafkaOptions).producer()
        return producer

After a successfull build you can use that from your nodejs code.