Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Client is not a constructor - error at the latest version of kafka-node

Mosca gives this error:

TypeError: Client is not a constructor
    at new KafkaAscoltatore (/home/x/Desktop/broker/node_modules/mosca/node_modules/ascoltatori/lib/kafka_ascoltatore.js:59:26)
    at Object.build (/home/x/Desktop/broker/node_modules/mosca/node_modules/ascoltatori/lib/ascoltatori.js:77:12)
    at /home/x/Desktop/broker/node_modules/mosca/lib/server.js:181:40
    at makeCall (/home/x/Desktop/broker/node_modules/fastseries/series.js:117:7)
    at ResultsHolder.release (/home/x/Desktop/broker/node_modules/fastseries/series.js:96:9)
    at series (/home/x/Desktop/broker/node_modules/fastseries/series.js:39:14)
    at Object.series (/home/x/Desktop/broker/node_modules/steed/steed.js:90:7)
    at new Server (/home/x/Desktop/broker/node_modules/mosca/lib/server.js:171:9)
    at Object.<anonymous> (/home/x/Desktop/broker/broker.js:118:14)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:266:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:596:3)

Environment

  • Node version:v10.9.0
  • Kafka-node version:latest
  • Mosca version:latest

What I have tried

I opened /home/x/Desktop/broker/node_modules/mosca/node_modules/ascoltatori/lib/kafka_ascoltatore.js and after looking at the line below found out that the problem is related with kafka-node version:

this._opts.kafka = this._opts.kafka || require("kafka-node");

When I removed this._opts.kafka, it started to work fine. Then I printed both I figured out that the new version does not have Client constructor in it. Here is my print result:

this._opts.kafka (kafka-node v4.0.1 latest) returns HighLevelProducer which does not have Client, on the other hand require("kafka-node") (v0.5.9) returns HighLevelConsumer.

Code:

var backend = {
    type: "kafka",
    kafka: require('kafka-node'),
    json: false,
    connectionString: "IP:2181",
    defaultEncoding: "utf8",
};

var moscaSettings = {
    interfaces: [
        { type: "mqtt", port: PORT }
    ],
    id: "mosca",
    stats: false,
    publishNewClient: false,
    publishClientDisconnect: false,
    publishSubscriptions: false,
    backend: backend,
};
var server = new mosca.Server(moscaSettings); // Error pops here

I could make Kafka to work with older version as I mentioned above, but I think it would be only a quick fix, so I hope somebody can lead me with a proper solution.

like image 904
Turhan Huseynov Avatar asked Feb 05 '19 11:02

Turhan Huseynov


1 Answers

The problem resulted from my using an example from the old version of kafka-node. When I replaced the code with the new example from the kafka-node GitHub, the problem was resolved.

like image 57
Turhan Huseynov Avatar answered Oct 26 '22 08:10

Turhan Huseynov