# mqttr

> A routable mqtt library based on mqtt.js

Latest version **3.1.0** (published 2023-01-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install mqttr
pnpm add mqttr
yarn add mqttr
bun add mqttr
```

## 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 | 3.1.0 |
| Published | 2023-01-17 |
| First published | 2015-12-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 460.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | taoyuan |
| Maintainers | towyuan |
| Keywords | mqtt, router, client |

## Links

- npm: https://www.npmjs.com/package/mqttr
- Repository: https://github.com/taoyuan/mqttr
- npm.io page: https://npm.io/package/mqttr

## Dependencies (6)

- [bl](https://npm.io/package/bl.md) ^6.0.0
- [debug](https://npm.io/package/debug.md) ^4.3.4
- [tslib](https://npm.io/package/tslib.md) ^2.4.1
- [msgpack5](https://npm.io/package/msgpack5.md) ^6.0.2
- [async-mqtt](https://npm.io/package/async-mqtt.md) ^2.6.3
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^6.2.1

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 3.1.0 (latest) — 2023-01-17
- 3.0.6 — 2022-01-20
- 3.0.5 — 2020-09-03
- 3.0.4 — 2020-09-03
- 3.0.3 — 2020-07-29
- 3.0.2 — 2020-07-29
- 3.0.1 — 2020-07-29
- 3.0.0-alpha.1 — 2020-07-28
- 3.0.0-alpha.0 — 2020-07-28
- 2.0.7 — 2018-10-03
- 2.0.6 — 2018-10-02
- 2.0.5 — 2018-09-27
- 2.0.3 — 2018-09-27
- 2.0.2 — 2018-09-27
- 2.0.1 — 2018-09-27
- … 24 more at https://npm.io/package/mqttr/versions

## README

# mqttr

[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url]
[![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url]

> A routable mqtt library based on mqtt.js

## Installation

```sh
$ npm i mqttr
```

## Usage

```typescript
import {connect, Message} from 'mqttr';

// eslint-disable-next-line  @typescript-eslint/no-floating-promises
(async () => {
  // You should start a mqtt server at 1883 before or after run this script
  const client = connect('mqtt://localhost');

  client.on('connect', function () {
    console.log('connect');
  });

  client.on('reconnect', function () {
    console.log('reconnect');
  });

  client.on('close', function () {
    console.log('close');
  });

  client.on('offline', function () {
    console.log('offline');
  });

  client.on('error', function (err: Error) {
    throw err;
  });

  // full params handler
  await client.subscribe(
    '/users/:userId/message/:messageId/:splats*',
    (topic: string, payload: any, message?: Message) => {
      message = message!;
      console.log('-------------------------------------------------');
      console.log('topic  :', topic); // => /users/yvan/message/4321/ping
      console.log('message:', payload); // => { hello: '🦄' }
      console.log('params :', message.params); // => { userId: 'yvan', messageId: 4321, splats: [ 'ping' ] }
      console.log('path   :', message.path); // => '/users/:userId/message/:messageId/:splats*'
      console.log('packet :', message.packet); // => {...} packet received packet, as defined in mqtt-packet
      console.log();
    },
  );

  // one context param handler
  await client.subscribe('/users/:userId/message/:messageId/:splats*', (message: Message) => {
    console.log('-------------------------------------------------');
    console.log(message);
    console.log();
  });

  await client.ready();

  await client.publish('/users/yvan/message/4321/ping', {hello: '🦄'});

  // eslint-disable-next-line @typescript-eslint/no-misused-promises
  setTimeout(() => client.end(true), 10);
})();
```

## Topic Patterns

See [path-to-regexp](https://github.com/pillarjs/path-to-regexp)

## License

MIT © [taoyuan](towyuan#outlook.com)

[npm-image]: https://badge.fury.io/js/mqttr.svg
[npm-url]: https://npmjs.org/package/mqttr
[travis-image]: https://travis-ci.org/taoyuan/mqttr.svg?branch=master
[travis-url]: https://travis-ci.org/taoyuan/mqttr
[daviddm-image]: https://david-dm.org/taoyuan/mqttr.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/taoyuan/mqttr
[coveralls-image]: https://coveralls.io/repos/taoyuan/mqttr/badge.svg
[coveralls-url]: https://coveralls.io/r/taoyuan/mqttr

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