diff --git a/nodes/core/io/10-mqtt.js b/nodes/core/io/10-mqtt.js index 4e65bd39b..9085505c7 100644 --- a/nodes/core/io/10-mqtt.js +++ b/nodes/core/io/10-mqtt.js @@ -45,21 +45,26 @@ module.exports = function(RED) { this.status({fill:"red",shape:"ring",text:"disconnected"}); this.client = connectionPool.get(this.brokerConfig.broker,this.brokerConfig.port,this.brokerConfig.clientid,this.brokerConfig.username,this.brokerConfig.password); var node = this; - this.client.subscribe(this.topic,2,function(topic,payload,qos,retain) { - if (isUtf8(payload)) { payload = payload.toString(); } - var msg = {topic:topic,payload:payload,qos:qos,retain:retain}; - if ((node.brokerConfig.broker === "localhost")||(node.brokerConfig.broker === "127.0.0.1")) { - msg._topic = topic; - } - node.send(msg); - }); - this.client.on("connectionlost",function() { - node.status({fill:"red",shape:"ring",text:"disconnected"}); - }); - this.client.on("connect",function() { - node.status({fill:"green",shape:"dot",text:"connected"}); - }); - this.client.connect(); + if (this.topic) { + this.client.subscribe(this.topic,2,function(topic,payload,qos,retain) { + if (isUtf8(payload)) { payload = payload.toString(); } + var msg = {topic:topic,payload:payload,qos:qos,retain:retain}; + if ((node.brokerConfig.broker === "localhost")||(node.brokerConfig.broker === "127.0.0.1")) { + msg._topic = topic; + } + node.send(msg); + }); + this.client.on("connectionlost",function() { + node.status({fill:"red",shape:"ring",text:"disconnected"}); + }); + this.client.on("connect",function() { + node.status({fill:"green",shape:"dot",text:"connected"}); + }); + this.client.connect(); + } + else { + this.error("topic not defined"); + } } else { this.error("missing broker configuration"); }