How to use mqtt

Comprehensive mqtt code examples:

How to use mqtt.disconnect:

151
152
153
154
155
156
157
158
          );
        }
      }
    });

    mqtt.disconnect();
  });
};

How to use mqtt.js:

14
15
16
17
18
19
20
21
22
23
  details. A Connection is a Writable stream, so you can run
  MQTT.js over any kind of Stream (doc needed). Both the constructors of
  MqttClient and MqttConnection changed, but not the factory method
  `mqtt.createClient` and `mqtt.createConnection`.

* v0.2.0 has brough some API breaking changes to mqtt.js. 
  Please consult the [migration guide](http://github.com/adamvr/MQTT.js/wiki/migration) for information
  or open an issue if you need any help.

## Installation

How to use mqtt.createSecureClient:

46
47
48
49
50
51
52
53
54
55
    certPath: crypto.getUserSSLCert(),
    ca: calist
  };
  console.log("Connecting securely to MQTT broker at " + serviceSpec.properties.secureaddress + ":" +
    serviceSpec.properties.secureport);
  this.client = mqtt.createSecureClient(serviceSpec.properties.secureport, serviceSpec.properties.secureaddress,
    opts);
} else {
  this.client = mqtt.createClient(serviceSpec.port, serviceSpec.address);
}

How to use mqtt.publish:

135
136
137
138
139
140
141
142
143
144
var
  dataPoint = reading.values,
  outputTopic = config.mqtt.outputTopic + "/" + reading.device_id;

if (dataPoint.t > config.alarm.maxTemp || dataPoint.p > config.alarm.maxPressure) {
  mqtt.publish(
    outputTopic, { alarm: 1, values: dataPoint }
  );
} else {
  alarm.train(trainingData);

How to use mqtt.MqttClient:

How to use mqtt.createClient:

49
50
51
52
53
54
55
56
57
58
  console.log("Connecting securely to MQTT broker at " + serviceSpec.properties.secureaddress + ":" +
    serviceSpec.properties.secureport);
  this.client = mqtt.createSecureClient(serviceSpec.properties.secureport, serviceSpec.properties.secureaddress,
    opts);
} else {
  this.client = mqtt.createClient(serviceSpec.port, serviceSpec.address);
}

this.spec = serviceSpec;
this.client.subscribe(this.spec.name);

How to use mqtt.Client:

130
131
132
133
134
135
136
137
138
139
if (!argv.topic) {
    console.log('No AWS IOT topic specified. Using teslams/{id} where {id} is the vehicle id of the car');
    argv.topic = 'teslams';
}
//
// Device is an instance returned by mqtt.Client(), see mqtt.js for full
// documentation.
//
device.on('connect', function() {
    ulog('awsiot device connected!');

How to use mqtt.connectAsync:

83
84
85
86
87
88
89
90
91
92
                ];
            }

async execute(runningFlow) {

        const client = await mqtt.connectAsync(this.url);
        console.log("Mqtt connect");

        await new Promise((resolve, reject) => {
            client.on('connect', function () {

How to use mqtt.connect:

25
26
27
28
29
30
31
32
33
34
var harmonyDeviceUpdateInterval = 1*60*1000 // 1 minute
var harmonyDeviceUpdateTimers = {}

var mqttClient = config.hasOwnProperty("mqtt_options") ?
    mqtt.connect(config.mqtt_host, config.mqtt_options) :
    mqtt.connect(config.mqtt_host);
var TOPIC_NAMESPACE = config.topic_namespace || "harmony-api";

var enableHTTPserver = config.hasOwnProperty("enableHTTPserver") ?
    config.enableHTTPserver : true;