Manages connections to a MQTT broker
Manages connections of the yaha Mqtt broker and provides functionality to receive/send messages from/to the managed connections in three service levels ( qos 0 , 1 , 2 )
- Meta
- Class Connections
- Parameters
- Methods
- acknowledgeMessage
- checkToken
- cleanup
- connect
- disconnect
- getAllMessagesToSend
- isDuplicateQos2message
- onPubrel
- persist
- publishMessage
- restoreFromFile
- setWill
- subscribe
- unsubscribe
| | |
| --- | --- |
| File | connections.js |
| Abstract | Manages connections of the yaha Mqtt broker and provides functionality to receive/send messages from/to the managed connections in three service levels ( qos 0 , 1 , 2 ) |
| 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 . |
new Connections(configuration)
Creates a connection object This class is part of an MQTT style broker . It manages connections to the broker .
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| configuration | Object | configuration | |
#### configuration properties
| Name | Type | Attribute | Default | Description |
| ---------- | ------------ | ------------ | ------------ | ----------------- |
| fileName | string | | | filename for persistence | |
| directory | string | | | directory ( file path ) for persistence | |
| replyTimeoutInMilliseconds | nubmber | optional | 10000 | timeout to wait for an answer of a http call | |
| inFlightWindow | number | optional | 1 | amount of qos 1 and qos 2 messages send for the same topic at the same time | |
| pubrelTimeoutInMilliseconds | number | optional | 24\3600\1000 | timout to wait for a corresponding pubrel message , default one day | |
| maxRetryCount | number | optional | 10 | amount of resend of messages before forcing a disconnect | |
| maxQueuSize | number | | | maximal amount of entries in a message queue | |
#### acknowledgeMessage
acknowledgeMessage (queueEntry)
Acknowledges a message ( qos = 1 or 2 only )
##### acknowledgeMessage Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| queueEntry | MessageQueueEntry | entry of message queue acknowledged | |
#### checkToken
checkToken (token) => {string, undefined}
Checks , if a token corresponds to a client . Returns the client
##### checkToken Parameters
| Name | Type |
| ---------- | ------------ |
| token | string | |
##### checkToken throws
| Type | Description |
| ---- | ----------- |
| string | error message |
##### checkToken returns
| Type | Description |
| ---- | ----------- |
| string, undefined | clientId . |
#### cleanup
cleanup () => {Object}
Clean up jobs . Call regularly ( example once a minute ) Automatically disconnects clients not sending data until timeout
##### cleanup returns
| Type | Description |
| ---- | ----------- |
| Object | list of clientId beeing automatically disconnected . An object with reason : string and keepAlive : number is included for each disconnected clientId |
#### connect
connect (clientId, host, port, clean, version, keepAlive) => {Object}
Connects a client ( Stores a connection to a client )
##### connect Parameters
| Name | Type | Attribute | Default | Description |
| ---------- | ------------ | ------------ | ------------ | ----------------- |
| clientId | any | | | id of the client | |
| host | string | | | host name | |
| port | string | | | port name | |
| clean | bool | | | true , if the connection will be cleaned on disconnect | |
| version | string | optional | '1 . 0' | client interface version | |
| keepAlive | number | optional | 3600000 | keep alive time in milliseconds . The broker will disconnect any client not sending messages in the keepAlive interval . Default is one hour | |
##### connect throws
| Type | Description |
| ---- | ----------- |
| Error | |
##### connect returns
| Type | Description |
| ---- | ----------- |
| Object | { mqttcode , present } present = = 1 , if an old session is recovered |
#### disconnect
disconnect (clientId)
Disconnects a client
##### disconnect Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| clientId | string | id of the client | |
##### disconnect throws
| Type | Description |
| ---- | ----------- |
| Error | |
#### getAllMessagesToSend
getAllMessagesToSend () => {integer}
Gets all messages to send for all clients function to be called for each message . The function has the parameter QoS and message
##### getAllMessagesToSend returns
| Type | Description |
| ---- | ----------- |
| integer | amount of messages sent |
#### isDuplicateQos2message
isDuplicateQos2message (clientId, topic, qos, dup, packetid)
Checks and remembers a qos2 message . Returns true , if the message is a duplicate
##### isDuplicateQos2message Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| clientId | string | id of the client | |
| topic | string | topic sent | |
| qos | Object | quality of service received from published headers | |
| dup | boolean | true , if the message is a duplicate | |
| packetid | string | id of the sender package | |
#### onPubrel
onPubrel (token, packetid)
Handles a pubrel message , deleting the entry from the qos2 queue
##### onPubrel Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| token | string | client send token | |
| packetid | number | unique identifier of the corresponding publish packet | |
#### persist
async persist ()
Persists all connections
#### publishMessage
publishMessage (payload, qos, retain)
Adds messages that needs to be published
##### publishMessage Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| payload | Message | message to add to a publish storage | |
| qos | Object | quality of service received from published headers | |
| retain | boolean | true , if the message should be retained for future subscribers | |
#### restoreFromFile
async restoreFromFile ()
Restores all clients from last valid saved file
#### setWill
setWill (clientId, will)
Sets the will message of a client
##### setWill Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| clientId | string | id of the client | |
| will | Object | will message configuration | |
#### will properties
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| qos | number | will message quality of service | |
| retain | boolean | true , if the will message is retained | |
| topic | string | topic of the will message | |
| value | string, number | value of the will message | |
#### subscribe
subscribe (clientId, topics) => {Array.
Subscribes to topics
##### subscribe Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| clientId | string | id of the client | |
| topics | Object | { QoS , topics } or { topic1 : QoS1 , topic2 : QoS2 , . . . } | |
##### subscribe returns
| Type | Description |
| ---- | ----------- |
| Array. | array of QoS values |
#### unsubscribe
unsubscribe (clientId, topics)
Unsubsribes to topics
##### unsubscribe Parameters
| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| clientId | string | id of the client | |
| topics | Array. | array of topics strings ( topic with wildchars '+' and '#' ) | |