2.3.1 • Published 1 year ago

@mangar2/mqttclient v2.3.1

Weekly downloads
9
License
LGPL-3.0-or-later
Repository
github
Last release
1 year ago

Abstract

Provides a standard client to communicate with the mqtt broker

Contents

Meta

Filemqttclient.js
AbstractProvides a standard client to communicate with the mqtt broker
AuthorVolker Böhm
CopyrightCopyright ( c ) 2020 Volker Böhm
LicenseThis 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 .

Type definitions

LogPattern {Object}

NameTypeDescription
topicstringlog topic pattern
modulestringmodule to log , "send" , "receive" or "all" ( for all modules )
levelnumberlog level for the pattern

Callback definitions

PollCallback

Callback to retrieve messages to be sent to the mqtt broker

PollCallback returns

TypeDescription
Array.<Message>, Messagemessages to send to the broker

Class MqttClient

new MqttClient(options)

Creates a standard mqtt client , connects and subscribes to the broker

MqttClient Parameters

NameTypeDescription
optionsObjectoptions to provide for connection

options properties

NameTypeAttributeDefaultDescription
clientIdstringunique id of the client
brokerObjectinformation of the broker to connect to
listenernumberport this client will listen to
versionstringoptional'1 . 0'interface version
keepAliveInSecondsstringoptionalconnection keep alive time in seconds
cleanbooleanoptionaltrueclean the broker session on disconnect
retrynumberamount of retries to send messages to the broker
logArray.<LogPattern>logging settings

broker properties

NameTypeDescription
hoststringhostname of the broker
portnumberport of the broker

MqttClient Members

NameTypedescription
connectedbooleanConnection status . true , iff connected
tokenObject.<send:string, receive:string>Send and receive token to be used to communicate with the mqtt broker
versionstringGets/Sets the interface version to use

MqttClient Methods

_createMemoryUsageMessage

_createMemoryUsageMessage () => {Message}

Creates a message showing the memory usage

_createMemoryUsageMessage returns
TypeDescription
Messagethe memory usage message with the topic $SYS/ clientId /memory usage

close

async close ()

closes the client by shutting down all services and loops

isShuttingDown

isShuttingDown () => {boolean}

checks , if the client is shutting down . Every loop must stop once isShuttingDown is true

isShuttingDown returns
TypeDescription
booleantrue , iff shutting down

on

on (event, callback)

Sets a callback .

on Parameters
NameTypeDescription
eventstringsupports 'shutdown'
callbackfunction
on throws
TypeDescription
Errorif the event is not supported
Errorif the callback is not 'function'

publish

publish (message, serviceName)

Publishes a message to the broker

publish Parameters
NameTypeDescription
messageMessagemessage to publish
serviceNamestringname of the publishing service

message properties

NameTypeDescription
qosnumberquality of service ( 0 , 1 , 2 )
retainbooleantrue to create a retain message

reconnect

async reconnect ()

Connects and subscribes to the broker

registerCloseFunction

registerCloseFunction (closeFunction)

Registers close functions . It will be called when the client close function is called

registerCloseFunction Parameters
NameTypeDescription
closeFunctionfunctionfunction to be called on close commands

registerRecipient

async registerRecipient (serviceName, subscriptions, callback)

Registeres a recipient

registerRecipient Parameters
NameTypeDescription
serviceNamestringname of the subscribing service
subscriptionsObjectsubscription entries of format { topic : qos , topic : qos , . . . }
callbackfunctionfunction to send received messages to
registerRecipient throws
TypeDescription
ErrorIf subscriptions are not well formatted or callback is not a function

registerSender

registerSender (intervalInMilliseconds, callback)

Registeres a service sending messages in intervals . The service must provide a function ( callback ) without parameters returing an array of messages . The messages will then be sent to the broker

registerSender Parameters
NameTypeDescription
intervalInMillisecondsnumberinterval in milliseconds to call the senders callback
callbackPollCallbackfunction to call

run

async run ()

Starts the mqttclient . Opens the listener and connects to the broker

updateSubscriptions

async updateSubscriptions (serviceName, subscriptions)

Updates the subscriptions

updateSubscriptions Parameters
NameTypeDescription
serviceNamestringname of the subscribing service
subscriptionsObjectsubscription entries of format { topic : qos , topic : qos , . . . }
updateSubscriptions throws
TypeDescription
ErrorIf subscriptions are not well formatted