@josefransaenz/hypergate-mqtt
v1.0.4
Published
hypergate-mqtt is built on top of [MQTT.js](https://github.com/mqttjs/MQTT.js) and can be used to connect a hypergate instance to a MQTT broker.
Downloads
7
Readme
hypergate-mqtt
hypergate-mqtt is built on top of MQTT.js and can be used to connect a hypergate instance to a MQTT broker.
API Reference
HypergateMqtt
Kind: global class
new HypergateMqtt(hypergate, baseTopic, url, options)
An instance of the HypergateMqtt class is a mqtt.js Client instance.
| Param | Type | Description | | --- | --- | --- | | hypergate | object | The instance of the Hypergate class to use | | baseTopic | string | string that will be appended to the topics related to the hypergate instance. The instance of the HypergateMqtt class will be subscribed to topic 'baseTopic/command/#' and will redirect the related messages to the hypergate.command method. All hypergate events will be automatically published as 'baseTopic/event/{original_hypergate_event}'. | | url | object | MQTT broker url | | options | object | MQTT.js options |
Example
const Hypergate = require('@josefransaenz/hypergate-core');
const HypergateMqtt = require('@josefransaenz/hypergate-mqtt');
const hypergate = new Hypergate(<YourPluginsSpecification>);
const baseTopic = <YourBaseTopicString>;
const mqttClient = new HypergateMqtt(hypergate, baseTopic, 'mqtt://test.mosquitto.org');
mqttClient.on('message', function (topic, message) {
console.log(`*** Topic: '${topic}', Payload: ${message.toString()}`);
});
mqttClient.on('connect', function () {
console.log('*** Connect');
});
mqttClient.on('close', () => {
console.log('** Disconect');
});