Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dersimn/node-mqtt-smarthome-connect


https://github.com/dersimn/node-mqtt-smarthome-connect

Last synced: about 14 hours ago
JSON representation

Awesome Lists containing this project

README

        

# node-mqtt-smarthome-connect

[![mqtt-smarthome](https://img.shields.io/badge/mqtt-smarthome-blue.svg)](https://github.com/mqtt-smarthome/mqtt-smarthome)
[![NPM version](https://badge.fury.io/js/mqtt-smarthome-connect.svg)](http://badge.fury.io/js/mqtt-smarthome-connect)
[![Build Status](https://travis-ci.org/dersimn/node-mqtt-smarthome-connect.svg?branch=master)](https://travis-ci.org/dersimn/node-mqtt-smarthome-connect)
[![Coverage Status](https://coveralls.io/repos/github/dersimn/node-mqtt-smarthome-connect/badge.svg?branch=master)](https://coveralls.io/github/dersimn/node-mqtt-smarthome-connect?branch=master)
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
[![License][mit-badge]][mit-url]

> Node module for MQTT-Smarthome

This Module is a wrapper around [MQTT.js](https://github.com/mqtt.js/mqtt.js) adding extra functionality to ease the
handling of topics and payloads following the
[mqtt-smarthome architecture proposal](https://github.com/mqtt-smarthome/mqtt-smarthome).

## Usage

`$ npm install mqtt-smarthome-connect --save`

```javascript
const mqsh = new MqttSmarthome('mqtt://localhost', {
will: {topic: 'foo/maintenance/online', payload: 'false', retain: true}
});

mqsh.on('connect', () => {
mqsh.publish('foo/maintenance/online', true, {retain: true});
});
mqsh.connect();

mqsh.subscribe('foo/+/bar', (topic, message, wildcardMatch, rawPacket) => {
console.log('received', wildcardMatch[0], message)
});

mqsh.publish('foo/boolean/bar', true);
mqsh.publish('foo/intNumber/bar', 42);
mqsh.publish('foo/floatNumber/bar', 3.1415);
mqsh.publish('foo/string/bar', 'A string.');
mqsh.publish('foo/array/bar', [1, 2, 3]);
mqsh.publish('foo/object/bar', {val: 42, ts: Date.now()});
```

See [examples](/examples) for more.

## API

## Classes


MqttSmarthome


## Typedefs



messageCallback : function


## MqttSmarthome
**Kind**: global class

* [MqttSmarthome](#MqttSmarthome)
* [new MqttSmarthome([mqttUrl], [options])](#new_MqttSmarthome_new)
* [.sub](#MqttSmarthome+sub) : MqttSmarthome.subscribe
* [.pub](#MqttSmarthome+pub) : MqttSmarthome.publish
* [.connect()](#MqttSmarthome+connect)
* [.end([force], [callback])](#MqttSmarthome+end)
* [.reconnect()](#MqttSmarthome+reconnect)
* [.subscribe(topic, [callback])](#MqttSmarthome+subscribe) ⇒ idSubscription
* [.unregisterCallback(id)](#MqttSmarthome+unregisterCallback) ⇒ number
* [.unsubscribe(topic, [callback])](#MqttSmarthome+unsubscribe)
* [.publish(topic, payload, [options], [callback])](#MqttSmarthome+publish)
* ["connect"](#MqttSmarthome+event_connect)
* ["close"](#MqttSmarthome+event_close)
* ["error"](#MqttSmarthome+event_error)
* ["offline"](#MqttSmarthome+event_offline)
* ["reconnect"](#MqttSmarthome+event_reconnect)
* ["message" (topic, payload, packet,)](#MqttSmarthome+event_message)

### new MqttSmarthome([mqttUrl], [options])
**Params**

- [mqttUrl] string = "mqtt://localhost"
- [options] object - see all available options in the [MQTT.js docs](https://github.com/mqttjs/MQTT.js#client)
- [.logger] object
- [.globalOptions] object - that'll overwrite options given in [publish(topic, payload, options, callback)](#MqttSmarthome+publish)
- [.clientId] string = "mqttsmarthome-<random>"

### mqttSmarthome.sub : MqttSmarthome.subscribe
Just a convenience alias to [subscribe](#MqttSmarthome+subscribe)

**Kind**: instance property of [MqttSmarthome](#MqttSmarthome)

### mqttSmarthome.pub : MqttSmarthome.publish
Just a convenience alias to [publish](#MqttSmarthome+publish)

**Kind**: instance property of [MqttSmarthome](#MqttSmarthome)

### mqttSmarthome.connect()
**Kind**: instance method of [MqttSmarthome](#MqttSmarthome)

### mqttSmarthome.end([force], [callback])
Disconnect from the MQTT broker.

**Kind**: instance method of [MqttSmarthome](#MqttSmarthome)
**Params**

- [force] boolean = false - passing it to true will close the client right away, without waiting for the in-flight messages to be acked.
- [callback] function - will be called when the client is closed.

### mqttSmarthome.reconnect()
Reconnect to the MQTT broker.

**Kind**: instance method of [MqttSmarthome](#MqttSmarthome)

### mqttSmarthome.subscribe(topic, [callback]) ⇒ idSubscription
**Kind**: instance method of [MqttSmarthome](#MqttSmarthome)
**Returns**: idSubscription - id
**Params**

- topic string
- [callback] [messageCallback](#messageCallback) =

### mqttSmarthome.unregisterCallback(id) ⇒ number
Unregister a callback. If no registered callback on the corresponding topic is left a MQTT unsubscribe will be
done.

**Kind**: instance method of [MqttSmarthome](#MqttSmarthome)
**Returns**: number - remaining number of subscription on that topic
**Params**

- id idSubscription - an id that was returned by the [subscribe()](#MqttSmarthome+subscribe) method.

### mqttSmarthome.unsubscribe(topic, [callback])
Unsubscribe a whole topic with all its callbacks.

**Kind**: instance method of [MqttSmarthome](#MqttSmarthome)
**Params**

- topic string
- [callback] function

### mqttSmarthome.publish(topic, payload, [options], [callback])
Publish a MQTT message. Payloads that are neither of type `string` nor an instance of `Buffer` will be JSON
stringified.

**Kind**: instance method of [MqttSmarthome](#MqttSmarthome)
**Params**

- topic string
- payload \*
- [options] object
- [.qos] number = 0 - QoS level
- [.retain] boolean = false - Retain Flag
- [.dup] boolean = false - Mark as duplicate flag
- [callback] function - Fired when the QoS handling completes, or at the next tick if QoS 0. An error occurs if client is disconnecting.

### "connect"
**Kind**: event emitted by [MqttSmarthome](#MqttSmarthome)

### "close"
**Kind**: event emitted by [MqttSmarthome](#MqttSmarthome)

### "error"
**Kind**: event emitted by [MqttSmarthome](#MqttSmarthome)

### "offline"
**Kind**: event emitted by [MqttSmarthome](#MqttSmarthome)

### "reconnect"
**Kind**: event emitted by [MqttSmarthome](#MqttSmarthome)

### "message" (topic, payload, packet,)
**Kind**: event emitted by [MqttSmarthome](#MqttSmarthome)
**Params**

- topic string
- payload string
- packet, Mqtt.packet - see https://github.com/mqttjs/mqtt-packet#publish

## messageCallback : function
**Kind**: global typedef
**Params**

- topic string
- payload string | number | boolean | object
- [wildcardMatch] array - If subscription was example/+/foo/bar this array contains the "+" in topic string
- packet Mqtt.packet

## License

MIT © [Simon Christmann](https://github.com/dersimn), [Sebastian Raff](https://github.com/hobbyquaker) and Contributors

[mit-badge]: https://img.shields.io/badge/License-MIT-blue.svg?style=flat
[mit-url]: LICENSE