-
Couldn't load subscription status.
- Fork 1.4k
client
The primary method of instantiating MqttClient is through
the factory method mqtt.createClient or
mqtt.createSecureClient for a tls secured client.
It is possible to instantiate MqttClient using
new MqttClient(...), but it requires the user to generate
their own underlying stream (such as a net.Socket). Thus,
it is recommended that the factory methods be used
The MqttClient class represents a client connection to an
MQTT broker over an arbitrary transport method (TCP, UDP,
TLS etc.). It makes heavy use of MqttConnection (see:
MqttConnection).
MqttClient automatically handles the following:
- Regular server pings
- QoS flow
- Default arguments to
MqttConnectionmethods
Instantiate a new instance of MqttClient
-
streamBuilderis a function that returns a subclass of theStreamclass that supports theconnectevent. Typically anet.Socket. -
optionsis the client connection options (see: MqttConnection#connect). Defaults:-
keepalive:10 -
clientId:'mqttjs'_ + crypto.randomBytes(16).toString('hex') -
protocolId:'MQIsdp' -
protocolVersion:3 -
encoding:'utf8'(set to'binary'for receiving binary payloads)
-
Publish a message
-
topicis the topic to publish to,String -
messageis the message to publish,BufferorString -
optionsis the options to publish with, including:-
qosqos level -
retainretain flag
-
-
callbackcallback fired when the QoS handling completes
Subscribe to a topic or topics
-
topicis aStringtopic to subscribe to or anArrayof topics to subscribe to. -
optionsis the options to subscribe with, including:-
qosqos subscription level
-
-
callback-function(err, granted)callback fired on suback where:-
erra subscription error -
grantedis an array of{topic, qos}where:-
topicis a subscribed to topic -
qosis the granted qos level on it
-
-
Unsubscribe from a topic or topics
-
topicis aStringtopic or an array of topics to unsubscribe from -
callbackfired on unsuback
Close connection - send a disconnect packet and close the underlying stream
function() {}
Emitted on successful connection (i.e. connack rc=0)
function(topic, message, packet) {}
Emitted when the client recieves a publish packet
-
topictopic of the received packet -
messagepayload of the received packet -
packetreceived packet, as used in MqttConnection