From d55e79c4f5923b1a4723842374081d4686a4ce1e Mon Sep 17 00:00:00 2001 From: Cornelius Suermann Date: Fri, 25 Oct 2024 17:35:08 +0200 Subject: [PATCH] fix(connection): Retry establishing a connection when Node-RED starts up offline; fixes #265 --- MqttClient.js | 6 +++++- connection.js | 7 +++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/MqttClient.js b/MqttClient.js index f7b5e8e..3a11880 100644 --- a/MqttClient.js +++ b/MqttClient.js @@ -54,7 +54,11 @@ class MqttClient extends EventEmitter { } async end() { - return this.client.endAsync() + try { + return this.client.endAsync() + } catch (err) { + return false + } } async publish(topic, json) { diff --git a/connection.js b/connection.js index d441998..be50eb1 100644 --- a/connection.js +++ b/connection.js @@ -726,10 +726,13 @@ module.exports = function (RED) { return } } catch (e) { - this.errorCode = - 'version check failed. Ensure internet connectivity and restart the flow' + this.errorCode = 'Connection failed' this.isError = true this.refreshChildrenNodeStatus() + + //retry again in 30s: + setTimeout(() => this.connectAndSubscribe(), 30_000) + return this.logger(`version check failed! ${e.message}`, null, 'error') }