From 4349b58dc582ea5b1aba9fa5b908916bdeb3ccb9 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Fri, 6 Mar 2026 15:00:03 +0000 Subject: [PATCH] Only connect websocket client if need to to address Issue #5506 --- .../nodes/core/network/22-websocket.js | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/network/22-websocket.js b/packages/node_modules/@node-red/nodes/core/network/22-websocket.js index 3c877dc79..927aafdfa 100644 --- a/packages/node_modules/@node-red/nodes/core/network/22-websocket.js +++ b/packages/node_modules/@node-red/nodes/core/network/22-websocket.js @@ -67,7 +67,7 @@ module.exports = function(RED) { } } - function startconn() { // Connect to remote endpoint + node.startconn = function() { // Connect to remote endpoint node.tout = null; const prox = getProxyForUrl(node.brokerurl, RED.settings.proxyOptions); let agent = undefined; @@ -96,15 +96,15 @@ module.exports = function(RED) { const keyValue = header.keyValue; const valueType = header.valueType; const valueValue = header.valueValue; - + const headerName = keyType === 'other' ? keyValue : keyType; let headerValue; - + switch(valueType){ case 'other': headerValue = valueValue; break; - + case 'env': headerValue = RED.util.evaluateNodeProperty(valueValue,valueType,node); break; @@ -165,7 +165,7 @@ module.exports = function(RED) { } if (!node.closing && !node.isServer) { clearTimeout(node.tout); - node.tout = setTimeout(function() { startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ? + node.tout = setTimeout(function() { node.startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ? } }); socket.on('message',function(data,flags) { @@ -176,7 +176,7 @@ module.exports = function(RED) { node.emit('erro',{err:err,id:id}); if (!node.closing && !node.isServer) { clearTimeout(node.tout); - node.tout = setTimeout(function() { startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ? + node.tout = setTimeout(function() { node.startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ? } } socket.on('error',socket.nrErrorHandler); @@ -229,7 +229,7 @@ module.exports = function(RED) { } else { node.closing = false; - startconn(); // start outbound connection + // node.startconn(); // start outbound connection } node.on("close", function(done) { @@ -245,7 +245,7 @@ module.exports = function(RED) { else { node.closing = true; node.server.close(); - //wait 20*50 (1000ms max) for ws to close. + //wait 20*50 (1000ms max) for ws to close. //call done when readyState === ws.CLOSED (or 1000ms, whichever comes fist) const closeMonitorInterval = 20; let closeMonitorCount = 50; @@ -268,6 +268,10 @@ module.exports = function(RED) { WebSocketListenerNode.prototype.registerInputNode = function(/*Node*/handler) { this._inputNodes.push(handler); + // start connection if we are the first and we are a client + if (!this.isServer && this._inputNodes.length === 1) { + this.startconn(); + } } WebSocketListenerNode.prototype.removeInputNode = function(/*Node*/handler) { @@ -417,6 +421,10 @@ module.exports = function(RED) { status._session = {type:"websocket",id:event.id} node.status(status); }); + // start connection if we are the first and we are a client + if (!this.serverConfig.isServer && this.serverConfig._inputNodes.length === 0) { + this.serverConfig.startconn(); + } } this.on("input", function(msg, nodeSend, nodeDone) { var payload;