# mqtt-dispatch

> mqtt-dispatch

Latest version **1.0.16** (published 2022-05-14) · ISC license · 0 weekly downloads

## Install

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

## 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.16 |
| Published | 2022-05-14 |
| First published | 2022-05-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 3.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | meg768 |

## Links

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

## Dependencies (1)

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

## Recent versions

- 1.0.16 (latest) — 2022-05-14
- 1.0.15 — 2022-05-14
- 1.0.14 — 2022-05-14
- 1.0.13 — 2022-05-14
- 1.0.12 — 2022-05-14
- 1.0.11 — 2022-05-14

## README

# mqtt-dispatch
This is a dispatcher for MQTT.js [https://www.npmjs.com/package/mqtt] which
supports message routing for specific topics. 

You no longer need to parse the topic in the 'message' event from MQTT.js.
This module makes it easy to listen to specific events in your MQTT tree of events.

See example below.

## Example

```javascript

function example() {
	let Mqtt = require('mqtt');
	let MqttDispatch = require('mqtt-dispatch');

	// Connect to mosquittos test server
	let options = {
		host:"mqtt://test.mosquitto.org",
		port:1883
	};

	console.log(`Connecting to MQTT broker ${options.host}...`);

	// Connect to MQTT broker as usual.
	let client = Mqtt.connect(options.host, options);

	// Modify MQTT client to dispatch messages
	client = MqttDispatch(client);

	// Notify when connected
	client.on('connect', () => {
		console.log(`Connected to MQTT broker ${options.host} on port ${options.port}.`);
	});
	
	// Subscribe to the topic "Example"
	client.subscribe('Example/#');

	// Listen to specific topics
	client.on('Example/A', (message) => {
		console.log(`Message A is ${JSON.stringify(message)}`);
	});

	client.on('Example/B', (message) => {
		console.log(`Message B is ${JSON.stringify(message)}`);
	});
	
	// Listen to topics using paramaters
	client.on('Example/:name/:value', (message, args) => {
		console.log(`Message ${args.name}/${args.value} is ${JSON.stringify(message)}`);
	});
	
	// Publish 
	client.publish('Example/A', 'AA');
	client.publish('Example/B', 'BB');
	client.publish('Example/A/B', 'AABB');
}

example();
```

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