mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
fix(MQTT Node): Node hangs forever on failed connection (#10048)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { connectAsync, type IClientOptions, type MqttClient } from 'mqtt';
|
||||
import { randomString } from 'n8n-workflow';
|
||||
import { connect, type IClientOptions, type MqttClient } from 'mqtt';
|
||||
import { ApplicationError, randomString } from 'n8n-workflow';
|
||||
import { formatPrivateKey } from '@utils/utilities';
|
||||
|
||||
interface BaseMqttCredential {
|
||||
@@ -49,5 +49,23 @@ export const createClient = async (credentials: MqttCredential): Promise<MqttCli
|
||||
clientOptions.rejectUnauthorized = credentials.rejectUnauthorized;
|
||||
}
|
||||
|
||||
return await connectAsync(clientOptions);
|
||||
return await new Promise((resolve, reject) => {
|
||||
const client = connect(clientOptions);
|
||||
|
||||
const onConnect = () => {
|
||||
client.removeListener('connect', onConnect);
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
client.removeListener('error', onError);
|
||||
resolve(client);
|
||||
};
|
||||
|
||||
const onError = (error: Error) => {
|
||||
client.removeListener('connect', onConnect);
|
||||
client.removeListener('error', onError);
|
||||
reject(new ApplicationError(error.message));
|
||||
};
|
||||
|
||||
client.once('connect', onConnect);
|
||||
client.once('error', onError);
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user