2.6.3 • Published 2 years ago

async-mqtt v2.6.3

Weekly downloads
22,870
License
MIT
Repository
github
Last release
2 years ago

IMPORTANT: Make sure you handle rejections from returned promises because they won't crash the process

API

The API is the same as MQTT.js, except the following functions now return promises instead of taking callbacks

  • publish
  • subscribe
  • unsubscribe
  • end

Example

const MQTT = require("async-mqtt");

const client = MQTT.connect("tcp://somehost.com:1883");

// When passing async functions as event listeners, make sure to have a try catch block

const doStuff = async () => {

	console.log("Starting");
	try {
		await client.publish("wow/so/cool", "It works!");
		// This line doesn't run until the server responds to the publish
		await client.end();
		// This line doesn't run until the client has disconnected without error
		console.log("Done");
	} catch (e){
		// Do something about it!
		console.log(e.stack);
		process.exit();
	}
}

client.on("connect", doStuff);

Alternately you can skip the event listeners and get a promise.

const MQTT = require("async-mqtt");

run()

async function run() {
  const client = await MQTT.connectAsync("tcp://somehost.com:1883")

  console.log("Starting");
	try {
		await client.publish("wow/so/cool", "It works!");
		// This line doesn't run until the server responds to the publish
		await client.end();
		// This line doesn't run until the client has disconnected without error
		console.log("Done");
	} catch (e){
		// Do something about it!
		console.log(e.stack);
		process.exit();
	}
}

Wrapping existing client

const { AsyncClient } = require("async-mqtt");

const client = getRegularMQTTClientFromSomewhere();

const asyncClient = new AsyncClient(client);

asyncClient.publish("foo/bar", "baz").then(() => {
	console.log("We async now");
	return asyncClient.end();
});
astarte-device-sdkhomebridge-melcloud-controlhomebridge-denon-tvhomebridge-enphase-envoyhomebridge-xbox-tvhomebridge-lgwebos-tvhomebridge-openwebif-tv@everything-registry/sub-chunk-1175node-prehubclient@inorbit/edge-sdk@ibloat/homie-ts@flowbuild/workflow@getanthill/datastore@iotize/cli@iotize/device-cli.nodeusp-js@piscale/chat-react@prima/mqtt@pubgcorp/mqtt-handler@runnerty/executor-mqttjwt-mqttjwt-mqtt-clientjwt-mqtt-tls-client@saltcorn/mqtt@roylines/aws-iot-rtl-433@kabbi/routed-mqtt@kocdigital/kocdigital-platform360-service-sdk-nodejs@knfcz/node-mqttflow-kafka-pipelines@maticnetwork/dagger@maticnetwork/eth-dagger@microdrop/device-ui-plugin@mikelaferriere/core@mikelaferriere/homelab-core@mrobst/solar-edgehomebridge-deyehomebridge-linusne-myhomehomebridge-mqttsmokesensorhomebridge-omnilink-platformhomebridge-espresensehomebridge-enviroindoorhomebridge-enviroplushomebridge-envirourbanhomebridge-comelit-platformhomebridge-rinnai-touch-platformhomebridge-rinnai-touch-plugin@sebbo2002/vestaboard2mqttturbproxyjson2collectdmqtt@open-kappa/mqtt-runnerhomebridge-washingmachine-powhomebridge-yi-cameratotamconsequuntur@nebulae/backend-node-tools@nebulae/event-storetether-agent@lcbm/knot-fog-connector-fiware@kumologica/kumologica-contrib-mqttgrapewirehapi-mqtt-client@oi4/oi4-oec-service-conformity-validator@oi4/oi4-oec-service-logger@oliverflecke/homebridge-mqtt-lighthuds@octopan/door-botmwservicemicro-webservicewoovi-electron@zalastax/nolb-async-m@vango-sols/zulu-hub-clientnestjs-mq-transporternetwork-metrics-logger@scrypted/merakimt@scicad/device-ui-plugin@things-factory/integration-base@tidaly/mqtt@telecomsante/mqtt-client@solace-labs/backstage-plugin-ep-devp-backend-commonlogs-to-mqtt-publishermojo-plugin-mqtt-helper@uci/mqtt@zyzak/house-brainmqtt-client-konginsidemqtt-data-emulatormqtt-enginemqtt-handlermqtt-linesmqtt-stressmqtt4netatmomqtt4teleinfomqtt4apcaccessmqtt4frenchtoolsmqttr@tuxbox/homebridge-homeassistantminspectorarestocats@agilatech/lynxari-mqtt-devicebusnode@autocodingsystems/mqtt-stdin-publish@beuluis/thermal-mqttastic
2.6.3

2 years ago

2.6.2

2 years ago

2.6.1

4 years ago

2.6.0

4 years ago

2.5.0

4 years ago

2.4.2

5 years ago

2.4.1

5 years ago

2.4.0

5 years ago

2.3.0

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.0.0

6 years ago

1.0.1

7 years ago

1.0.0

7 years ago