# async-mqtt

> Promise wrapper over MQTT.js

Latest version **2.6.3** (published 2022-09-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install async-mqtt
pnpm add async-mqtt
yarn add async-mqtt
bun add async-mqtt
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.6.3 |
| Published | 2022-09-06 |
| First published | 2017-02-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 16.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 273 |
| Author | rangermauve |
| Maintainers | matteo.collina, rangermauve, tabrizian |
| Keywords | mqtt, promise, async, publish, subscribe |

## Links

- npm: https://www.npmjs.com/package/async-mqtt
- Repository: https://github.com/mqttjs/async-mqtt
- Homepage: https://github.com/mqttjs/async-mqtt#readme
- Issues: https://github.com/mqttjs/async-mqtt/issues
- npm.io page: https://npm.io/package/async-mqtt

## Dependencies (1)

- [mqtt](https://npm.io/package/mqtt.md) ^4.3.7

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 2.6.3 (latest) — 2022-09-06
- 2.6.2 — 2022-01-23
- 2.6.1 — 2020-06-29
- 2.6.0 — 2020-05-24
- 2.5.0 — 2020-02-03
- 2.4.2 — 2019-08-19
- 2.4.1 — 2019-08-13
- 2.4.0 — 2019-08-07
- 2.3.0 — 2019-05-31
- 2.2.1 — 2019-03-25
- 2.2.0 — 2019-02-25
- 2.0.0 — 2018-09-14
- 1.0.1 — 2017-03-10
- 1.0.0 — 2017-02-26

## README

<h1 align="center">async-mqtt</h1>
<p align="center">Promise wrapper over MQTT.js</p>
<p align="center">
<a href="https://github.com/mqttjs/async-mqtt">
    <img alt="" src="https://david-dm.org/mqttjs/async-mqtt.svg?style=flat-square">
</a>
<a href="https://www.npmjs.com/package/async-mqtt">
    <img alt="" src="https://img.shields.io/npm/dt/async-mqtt.svg?style=flat-square">
</a>
<a href="https://www.npmjs.com/package/async-mqtt">
    <img alt="" src="https://img.shields.io/npm/v/async-mqtt.svg?style=flat-square">
</a>
<br>
<a href="https://github.com/mqttjs/async-mqtt">
    <img alt="" src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square">
</a>
</p>

**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](https://github.com/mqttjs/MQTT.js#api), except the following functions now return promises instead of taking callbacks

- publish
- subscribe
- unsubscribe
- end


## Example

```javascript
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.

```js
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

```javascript
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();
});
```

---
_Source: https://npm.io/package/async-mqtt · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
