# mqtt-async

> Use MQTT with async/await

Latest version **1.0.46** (published 2023-04-19) · ISC license · 40 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.46 |
| Published | 2023-04-19 |
| First published | 2023-04-13 |
| Weekly downloads | 40 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Magnus Egelberg |
| Maintainers | meg768 |
| Keywords | mqtt, mqtt-async, async-mqtt, publish, subscribe, async |

## Links

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

## 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

- 1.0.46 (latest) — 2023-04-19
- 1.0.44 — 2023-04-16
- 1.0.43 — 2023-04-16
- 1.0.42 — 2023-04-16
- 1.0.41 — 2023-04-16
- 1.0.39 — 2023-04-16
- 1.0.38 — 2023-04-16
- 1.0.36 — 2023-04-16
- 1.0.34 — 2023-04-14
- 1.0.33 — 2023-04-14
- 1.0.32 — 2023-04-14
- 1.0.30 — 2023-04-14
- 1.0.28 — 2023-04-14
- 1.0.26 — 2023-04-14
- 1.0.24 — 2023-04-14
- … 13 more at https://npm.io/package/mqtt-async/versions

## README

# mqtt-async
A simple way to convert the [MQTT.js library](https://www.npmjs.com/package/mqtt) to use async/await. No dependecies. Just a simple function. 
This is a different approach than the [async-mqtt](https://www.npmjs.com/package/async-mqtt) library which encapsulates MQTT.

Create an MQTT instance as normal. Then just call MqttAsync().

```javascript
const Mqtt = require('mqtt');
const MqttAsync = require('mqtt-async');

let mqtt = MqttAsync(Mqtt.connect(...));

await mqtt.subscribe('topic');
await mqtt.publish('topic', 'Hello');

```

This is the complete source code. Nothing more. Just copy it into your project or use this module.

```javascript

module.exports = function MqttAsync(client) {

    let subscribe = client.subscribe;
    let publish = client.publish;
    let unsubscribe = client.unsubscribe;
    let end = client.end;

    client.publish = (...args) => {
        return new Promise((resolve, reject) => {
            publish.call(client, ...args, (error) => {
                error ? reject(error) : resolve();
            });
        });
    }

    client.subscribe = (...args) => {
        return new Promise((resolve, reject) => {
            subscribe.call(client, ...args, (error) => {
                error ? reject(error) : resolve();
            });
        });

    }

    client.unsubscribe = (...args) => {
        return new Promise((resolve, reject) => {
            unsubscribe.call(client, ...args, (error) => {
                error ? reject(error) : resolve();
            });
        });

    }

    client.end = (...args) => {
        return new Promise((resolve, reject) => {
            end.call(client, ...args, (error) => {
                error ? reject(error) : resolve();
            });
        });

    }

    return client;
}

```

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