MQTTClient.connect()

Overview

Causes the MQTTClient object to connect or reconnect with the settings given when the object was constructed.

Added in version 2.1.13

Format

var mqttClient = new MQTTClient(options);

mqttClient.connect(onComplete, onError);

Value Description Type Required Notes
onComplete A callback function that is called asynchronously if the connection completes. Function Optional When called, the first parameter is a Complete object as described below.
onError A callback function that is called asynchronously if the connection errors. Function Optional When called, the first parameter is a Error object as described below.

Complete object properties

Value Description Type

Notes

result A result code. Integer

Possible values:

0 – Success

128 – Unspecified error

129 – Malformed packet

130 – Protocol error

131 – Implementation specific error

132 – Unsupported protocol version

133 – Client identifier not valid

134 – Bad username or password

135 – Not authorized

136 – Server unavailable

137 – Server busy

138 – Banned

140 – Bad authentication method

144 – Topic name invalid

149 – Packet too large

151 – Quota exceeded

153 – Payload format invalid

154 – Retain not supported

155 – QoS not supported

156 – Use another server

157 – Server moved

159 – Connection rate exceeded

Error object properties

Value Description Type

Notes

message A message that describes the failure. String May be undefined.
type A category for the type of failure. String  

Example

See MQTTClient constructor for additional examples.

Copy
/* Displays a toast on success or failure to connect to an MQTT broker.
 */

mqttClient = new MQTTClient({
    broker: "mqtt://mymqtt.example.com"
});

mqttClient.connect(function(complete) {
    View.toast("successfully connected to mqtt server: " + complete.result, true);
}, function(error) {
    View.toast("failed to connected to mqtt server: " + (error.message || error.type), true);
});