@mangar2/mqttservice
Version:
communicates with a MQTT-Style HTTP broker
151 lines (100 loc) • 4.59 kB
Markdown
<!-- This file is generated by jsmddoc version 0.1 -->
# Abstract
Library module to communicate with the mqtt broker . It includes Publish to send messsages to the broker and OnPublish to receive messages published by the broker
## Contents
- [Meta](#Meta)
- [Class OnPublish](#Class-OnPublish)
- [Parameters](#OnPublish-Parameters)
- [Members](#OnPublish-Members)
- [Methods](#OnPublish-Methods)
- [close](#close)
- [listen](#listen)
- [on](#on)
- [Class PublishMessage](#Class-PublishMessage)
- [Parameters](#PublishMessage-Parameters)
- [Methods](#PublishMessage-Methods)
- [close](#close)
- [publish](#publish)
## Meta
| | |
| --- | --- |
| **File** | mqttservice.js |
| **Abstract** | Library module to communicate with the mqtt broker . It includes Publish to send messsages to the broker and OnPublish to receive messages published by the broker |
| **Author** | Volker Böhm |
| **Copyright** | Copyright ( c ) 2020 Volker Böhm |
| **License** | This software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3 . It is furnished "as is" , without any support , and with no warranty , express or implied , as to its usefulness for any purpose . |
## Callback definitions
### ProcessMessage
#### ProcessMessage Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `message` | `Object` | the message received | |
| `qos` | `0, 1, 2` | the quality of service information | |
| `dup` | `0, 1` | flag signaling duplicates | |
## Class OnPublish
`new OnPublish(listenerPort, logSettings, qos2PubrelTimeoutInSeconds)`
Creates a new server receiving messages Supports registering a callback with on ( 'publish' , ( payload , qos ) = > { } ) called for each received message
### OnPublish Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `listenerPort` | `number` | port to listen on | |
| `logSettings` | `Object` | logging settings | |
| `qos2PubrelTimeoutInSeconds` | `number` | time to delete qos2 related packageid´s from the qos2 queue . This will happen , if a pubrel does not follow the publish call for a this amount of seconds | |
### OnPublish Members
| Name | Type | description |
| ------------ | ------------ | ------------ |
| `logFilter` | `LogFilter` | Logging filter |
| `port` | `number` | Port the server uses , only defined after listen has been called ! |
### OnPublish Methods
#### close
`close ()`
Closes all connections
#### listen
`listen ()`
Starts to listen and wait for input
#### on
`on (event, callback)`
Sets a callback . Usually , there is no need to set anything here
##### on Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `event` | `string` | event name , supported 'publish' | |
| `callback` | `ProcessMessage` | callback to be called , to process messages received | |
## Class PublishMessage
`new PublishMessage(host, port, configuration)`
Creates a client for publishing messages
### Example
```javascript
//Publish the message with topic /a/a, value 1, reason "test", QoS 0, retain 0:
const publish = new PublishMessage('myhost', 10000, { retry: 60 })
result = await publish.publish(publishtoken, new Message("/a/a", 1, "test"), 0, 0);
```
### PublishMessage Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `host` | `string` | host name ( or ip ) | |
| `port` | `number` | port number | |
| `configuration` | `Object` | configuration options | |
#### configuration properties
| Name | Type | Attribute | Default | Description |
| ---------- | ------------ | ------------ | ------------ | ----------------- |
| `retry` | `number` | optional | 60 | amount of retries to send a message | |
### PublishMessage Methods
#### close
`async close ()`
Closes the connection to the broker
#### publish
`async publish (token, message, qos, retain, version)`
Publishes a message with a defined quality of service automatically generating an id
##### publish Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `token` | `string` | connection token | |
| `message` | `Object` | message to publish . | |
| `qos` | `number` | 0 , 1 , 2 quality of service | |
| `retain` | `boolean` | True , if message shall be retained for future subscriptions | |
| `version` | `string, undefined` | interface version , supports 0 . 0 and 1 . 0 ( default ) | |
##### publish throws
| Type | Description |
| ---- | ----------- |
| `string` | on any connection error |