MQTT Client
MQTT library for a JavaScript Plugin
Remarks
The example below is for a client connection to a broker that conforms to the Homie MQTT specification.
https://homieiot.github.io/
The example also demonstrates how raw data values can be mapped to Capabilities.
Example
Methods
connect
Connects to the broker specified by the given url and options.
connect([url], [options])
url
: The URL can be on the following protocols: 'mqtt', 'mqtts', 'tcp', 'tls', 'ws', 'wss'.options
host
: The host name or IP address of broker. Not needed when a URL is supplied.port
: The port of broker. Not needed when a URL is supplied.keepalive
:60
seconds, set to0
to disableclientId
: Defaults to a randomly generated UUIDprotocolVersion
:4
clean
:true
, set to false to receive QoS 1 and 2 messages while offlineusername
: the username required by your broker, if anypassword
: the password required by your broker, if anysessionExpiryInterval
: representing the Session Expiry Interval in secondsnumber
,receiveMaximum
: representing the Receive Maximum valuenumber
,maximumPacketSize
: representing the Maximum Packet Size the Client is willing to acceptnumber
,topicAliasMaximum
: representing the Topic Alias Maximum value indicates the highest value that the Client will accept as a Topic Alias sent by the Servernumber
,requestResponseInformation
: The Client uses this value to request the Server to return Response Information in the CONNACKboolean
,requestProblemInformation
: The Client uses this value to indicate whether the Reason String or User Properties are sent in the case of failuresboolean
,authenticationMethod
: the name of the authentication method used for extended authenticationstring
,authenticationData
: Binary Data containing authentication databinary
rejectUnauthorized
: Set to false to ignore certificate errors. Defaults to true.
disconnect
Close connection to the MQTT server.
disconnect([options])
options
reasonString
: The reason for closing the connection.timeout
: Time, in milliseconds, to wait before timing out. The default is 0 which waits indefinitely.
publish
Publishes a message to a topic.
publish(topic, message, [options])
topic
: is the topic to publish to, String.message
: is the message to publish, Buffer or String.options
qos
: QoS level, Number, default 0.retain
: retain flag, Boolean, default false.dup
: mark as duplicate flag, Boolean, default false.payloadFormatIndicator
: Payload is UTF-8 Encoded Character Data or not boolean.messageExpiryInterval
: the lifetime of the Application Message in seconds number.topicAlias
: value that is used to identify the Topic instead of using the Topic Name number.responseTopic
: String which is used as the Topic Name for a response message string.correlationData
: used by the sender of the Request Message to identify which request the Response Message is for when it is received binary.userProperties
: The User Property is allowed to appear multiple times to represent multiple name, value pairs object.subscriptionIdentifier
: representing the identifier of the subscription number.contentType
: String describing the content of the Application Message string.timeout
: Time, in milliseconds, to wait before timing out. The default is 0 which waits indefinitely.
subscribe
Subscribe to a single topic or an array of topics.
subscribe(topics, message, [options])
topic
: is aString
topic to subscribe to or anArray
of topics to subscribe to. MQTTtopic
wildcard characters are supported (+
- for single level and#
- for multi level)options
is the options to subscribe with, including:qos
: QoS subscription level, default 0nl
: No Local MQTT 5.0 flag (If the value is true, Application Messages MUST NOT be forwarded to a connection with a ClientID equal to the ClientID of the publishing connection)rap
: Retain as Published MQTT 5.0 flag (If true, Application Messages forwarded using this subscription keep the RETAIN flag they were published with. If false, Application Messages forwarded using this subscription have the RETAIN flag set to 0.)rh
: Retain Handling MQTT 5.0 (This option specifies whether retained messages are sent when the subscription is established.)subscriptionIdentifier
: representing the identifier of the subscriptionnumber
,userProperties
: The User Property is allowed to appear multiple times to represent multiple name, value pairsobject
timeout
: Time, in milliseconds, to wait before timing out. The default is 0 which waits indefinitely.
unsubscribe
Unsubscribe from a topic or topics.
subscribe(topics, [options])
topic
is aString
topic or an array of topics to unsubscribe fromoptions
:userProperties
: The User Property is allowed to appear multiple times to represent multiple name, value pairsobject
timeout
: Time, in milliseconds, to wait before timing out. The default is 0 which waits indefinitely.
readMessage
Reads message from the message queue.
readMessage([options])
options
:timeout
: Time, in milliseconds, to wait before timing out. The default is 0 which waits indefinitely.
Returns & removes the oldest message in the queue. If the queue is empty, it'll wait/block until 1 is received.