From b5dfd62c992b186203368ec14a15965f31201b74 Mon Sep 17 00:00:00 2001 From: Tim Janke Date: Mon, 27 Feb 2023 12:21:39 +0100 Subject: [PATCH] Add an option to not unsubscribe topics on disconnect --- .../@node-red/nodes/core/network/10-mqtt.html | 19 +++++++++++++++++++ .../@node-red/nodes/core/network/10-mqtt.js | 19 ++++++++++++++----- .../@node-red/nodes/locales/de/messages.json | 1 + .../nodes/locales/en-US/messages.json | 1 + 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.html b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.html index bb76b33bc..c89550dc6 100644 --- a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.html +++ b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.html @@ -249,6 +249,12 @@ +
+ +
@@ -505,6 +511,7 @@ label: RED._("node-red:mqtt.label.keepalive"), validate:RED.validators.number(false)}, cleansession: {value: true}, + unsubscribeOnDisconnect: {value: true}, birthTopic: {value:"", validate:validateMQTTPublishTopic}, birthQos: {value:"0"}, birthRetain: {value:"false"}, @@ -620,6 +627,10 @@ this.cleansession = true; $("#node-config-input-cleansession").prop("checked",true); } + if (typeof this.unsubscribeOnDisconnect === 'undefined') { + this.unsubscribeOnDisconnect = true; + $("#node-config-input-unsubscribeOnDisconnect").prop("checked",true); + } if (typeof this.usetls === 'undefined') { this.usetls = false; $("#node-config-input-usetls").prop("checked",false); @@ -635,6 +646,14 @@ if (typeof this.protocolVersion === 'undefined') { this.protocolVersion = 4; } + $("#node-config-input-cleansession").on("change", function() { + var useCleanSession = $("#node-config-input-cleansession").is(':checked'); + if(useCleanSession) { + $("div.form-row.mqtt-persistence").hide(); + } else { + $("div.form-row.mqtt-persistence").show(); + } + }); $("#node-config-input-protocolVersion").on("change", function() { var v5 = $("#node-config-input-protocolVersion").val() == "5"; if(v5) { diff --git a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js index 0a7ed6dfa..6f757a4c0 100644 --- a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js +++ b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js @@ -482,6 +482,7 @@ module.exports = function(RED) { setIfHasProperty(opts, node, "protocolVersion", init); setIfHasProperty(opts, node, "keepalive", init); setIfHasProperty(opts, node, "cleansession", init); + setIfHasProperty(opts, node, "unsubscribeOnDisconnect", init); setIfHasProperty(opts, node, "topicAliasMaximum", init); setIfHasProperty(opts, node, "maximumPacketSize", init); setIfHasProperty(opts, node, "receiveMaximum", init); @@ -590,6 +591,9 @@ module.exports = function(RED) { if (typeof node.cleansession === 'undefined') { node.cleansession = true; } + if (typeof node.unsubscribeOnDisconnect === 'undefined') { + node.unsubscribeOnDisconnect = true; + } //use url or build a url from usetls://broker:port if (node.url && node.brokerurl !== node.url) { @@ -660,6 +664,7 @@ module.exports = function(RED) { node.options.password = node.password; node.options.keepalive = node.keepalive; node.options.clean = node.cleansession; + node.options.unsubscribeOnDisconnect = node.unsubscribeOnDisconnect; node.options.clientId = node.clientid || 'nodered_' + RED.util.generateId(); node.options.reconnectPeriod = RED.settings.mqttReconnectTime||5000; delete node.options.protocolId; //V4+ default @@ -1228,12 +1233,16 @@ module.exports = function(RED) { node.on('close', function(removed, done) { if (node.brokerConn) { if(node.isDynamic) { - Object.keys(node.dynamicSubs).forEach(function (topic) { - node.brokerConn.unsubscribe(topic, node.id, removed); - }); - node.dynamicSubs = {}; + if (node.brokerConn.options.unsubscribeOnDisconnect) { + Object.keys(node.dynamicSubs).forEach(function (topic) { + node.brokerConn.unsubscribe(topic, node.id, removed); + }); + node.dynamicSubs = {}; + } } else { - node.brokerConn.unsubscribe(node.topic,node.id, removed); + if (node.brokerConn.options.unsubscribeOnDisconnect) { + node.brokerConn.unsubscribe(node.topic, node.id, removed); + } } node.brokerConn.deregister(node, done, removed); node.brokerConn = null; diff --git a/packages/node_modules/@node-red/nodes/locales/de/messages.json b/packages/node_modules/@node-red/nodes/locales/de/messages.json index 65f251e98..9d6b927ac 100644 --- a/packages/node_modules/@node-red/nodes/locales/de/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/de/messages.json @@ -362,6 +362,7 @@ "port": "Port", "keepalive": "Keep-Alive", "cleansession": "Bereinigte Sitzung (clean session) verwenden", + "unsubscribeOnDisconnect": "Abonnement bei Verbindungsende automatisch beenden", "cleanstart": "Verwende bereinigten Start", "use-tls": "TLS", "tls-config": "TLS-Konfiguration", diff --git a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json index 66c492dc8..70bc1b888 100644 --- a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json @@ -414,6 +414,7 @@ "port": "Port", "keepalive": "Keep Alive", "cleansession": "Use clean session", + "unsubscribeOnDisconnect": "Automatically unsubscribe when disconnecting", "cleanstart": "Use clean start", "use-tls": "Use TLS", "tls-config": "TLS Configuration",