fix(MQTT Node): Close connection if connection attempt fails (#10873)

This commit is contained in:
Tomi Turtiainen
2024-09-18 20:03:18 +03:00
committed by GitHub
parent 0a317b7072
commit ee7147c6b3
3 changed files with 46 additions and 10 deletions

View File

@@ -1,5 +1,6 @@
import { connect, type IClientOptions, type MqttClient } from 'mqtt';
import { ApplicationError, randomString } from 'n8n-workflow';
import { formatPrivateKey } from '@utils/utilities';
interface BaseMqttCredential {
@@ -62,6 +63,10 @@ export const createClient = async (credentials: MqttCredential): Promise<MqttCli
const onError = (error: Error) => {
client.removeListener('connect', onConnect);
client.removeListener('error', onError);
// mqtt client has an automatic reconnect mechanism that will
// keep trying to reconnect until it succeeds unless we
// explicitly close the client
client.end();
reject(new ApplicationError(error.message));
};