I have been trying to resume the mqtt client connection, but the client disconnects immediately, i'm not able to subscribe to any messages further.
The 'close' event triggers immediately after the client gets exported.
import mqtt from "mqtt";
import jsonfile from "jsonfile";
import path from 'path';
let default_options = {
clientId: `cl_1234`,
clean: false,
keepalive: 10 * 1000,
reconnectPeriod: 90000,
connectTimeout: 30 * 1000,
username: "",
password: "",
};
//get broker info from the config file
let config = jsonfile.readFileSync(path.resolve(process.env.CONFIG));
//create a singleton, reuse the connection to broker
const client = mqtt.connect(`mqtt://${config.mqtt.host}`, Object.assign(default_options, config.mqtt.options));
client.on('connect', () => {
console.log(`|| Client - ${deviceId} - Connection to Broker OK !`);
});
client.on('reconnect', () => {
console.log(`|| Client - ${deviceId} - Connection to Broker Reconnected.`);
})
client.on('error', (err) => {
console.log(`|| Client - ${deviceId} - Connection to Broker Not Ok - ${err}!`)
});
client.on('close', () => {
console.log(`|| Client - ${deviceId} - Client Disconnected / Not able to connect to broker :( !`);
});
export {client};
via manju4ever
No comments:
Post a Comment