moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 513 forks source link

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

Closed turhan-huseynov closed 5 years ago

turhan-huseynov commented 5 years ago

Environment

Sample 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

Questions?

Mosca gives this error:

TypeError: Client is not a constructor
    at new KafkaAscoltatore (/home/tribot/Desktop/broker/node_modules/mosca/node_modules/ascoltatori/lib/kafka_ascoltatore.js:59:26)
    at Object.build (/home/tribot/Desktop/broker/node_modules/mosca/node_modules/ascoltatori/lib/ascoltatori.js:77:12)
    at /home/tribot/Desktop/broker/node_modules/mosca/lib/server.js:181:40
    at makeCall (/home/tribot/Desktop/broker/node_modules/fastseries/series.js:117:7)
    at ResultsHolder.release (/home/tribot/Desktop/broker/node_modules/fastseries/series.js:96:9)
    at series (/home/tribot/Desktop/broker/node_modules/fastseries/series.js:39:14)
    at Object.series (/home/tribot/Desktop/broker/node_modules/steed/steed.js:90:7)
    at new Server (/home/tribot/Desktop/broker/node_modules/mosca/lib/server.js:171:9)
    at Object.<anonymous> (/home/tribot/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)

So 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. I could make Kafka to work with older version but i think it would be only a quick fix, so i hope somebody can lead me with a proper solution.

turhan-huseynov commented 5 years ago

Looks like on new examples of kafka, we dont use this line kafka: require('kafka-node') it fixed my problem.

user1m commented 5 years ago

@Tribot91 can you elaborate? Running into the same issue when trying to use kafka-node v4+ Are you saying just remove the kafka: require('kafka-node') and it will work?

user1m commented 5 years ago

@Tribot91 can you point me to where in the docs you found this fix?