diff --git a/packages/node_modules/@node-red/nodes/core/io/10-mqtt.js b/packages/node_modules/@node-red/nodes/core/io/10-mqtt.js index 83132130d..f6702e902 100644 --- a/packages/node_modules/@node-red/nodes/core/io/10-mqtt.js +++ b/packages/node_modules/@node-red/nodes/core/io/10-mqtt.js @@ -315,7 +315,7 @@ module.exports = function(RED) { } }; - this.unsubscribe = function (topic, ref) { + this.unsubscribe = function (topic, ref, removed) { ref = ref||0; var sub = node.subscriptions[topic]; if (sub) { @@ -323,10 +323,12 @@ module.exports = function(RED) { node.client.removeListener('message',sub[ref].handler); delete sub[ref]; } - if (Object.keys(sub).length === 0) { - delete node.subscriptions[topic]; - if (node.connected) { - node.client.unsubscribe(topic); + if (removed) { + if (Object.keys(sub).length === 0) { + delete node.subscriptions[topic]; + if (node.connected) { + node.client.unsubscribe(topic); + } } } } @@ -430,10 +432,7 @@ module.exports = function(RED) { } this.on('close', function(removed, done) { if (node.brokerConn) { - if (removed) { - // This node has been removed so remove any subscriptions - node.brokerConn.unsubscribe(node.topic,node.id); - } + node.brokerConn.unsubscribe(node.topic,node.id, removed); node.brokerConn.deregister(node,done); } });