From 8e9169c1234bffc69b14cfad0f38e73ba1fb2739 Mon Sep 17 00:00:00 2001 From: Olivier Verhaegen <56387556+OlivierVerhaegen@users.noreply.github.com> Date: Fri, 30 Jun 2023 10:09:17 +0200 Subject: [PATCH] Bugfix: only execute node register callbacks after connection to the server has been made --- io/stomp/18-stomp.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/io/stomp/18-stomp.js b/io/stomp/18-stomp.js index 9b893a6e..698aab37 100644 --- a/io/stomp/18-stomp.js +++ b/io/stomp/18-stomp.js @@ -84,6 +84,8 @@ module.exports = function(RED) { node.sessionId = null; node.subscribtionIndex = 1; node.subscriptionIds = {}; + /** Array of callbacks to be called once the connection to the broker has been made */ + node.connectedCallbacks = []; /** @type { StompClient } */ node.client; node.setOptions = function(options, init) { @@ -134,10 +136,20 @@ module.exports = function(RED) { */ node.register = function(stompNode, callback = () => {}) { node.users[stompNode.id] = stompNode; + + if (!node.connected) { + node.connectedCallbacks.push(callback); + } + // Auto connect when first STOMP processing node is added if (Object.keys(node.users).length === 1) { - node.connect(callback); + node.connect(() => { + while (node.connectedCallbacks.length) { + node.connectedCallbacks.shift().call(); + } + }); } else { + // Execute callback directly as the connection to the STOMP server has already been made callback(); } }