I’m experimenting with using the JavaScript MQTT client instead of the REST client. I’m interested in using it because it offers automatic support for seeing when devices are connected to Losant. However, perhaps I’m misunderstanding what is defined as a “disconnection.” I am using this test program:
var Device = require("losant-mqtt").Device;
var device = new Device({
id: "my-id",
key: "my-key",
secret: "my-secret",
transport: "tls"
});
function send() {
device.sendState({ bottlesRemaining: 0 }, function(err) {
if (err) console.error(err);
else console.log("Success");
});
}
device.on("connect", function() {
setInterval(send, 1000);
console.log("Device connected");
});
device.on("offline", function() {
console.error("Device offline");
});
device.on("close", function() {
console.error("Device closed");
});
device.on("error", function() {
console.error("Device error");
});
device.on("reconnect", function() {
console.log("Reconnected");
})
device.connect(function(){});
I’m running this program in Node 4.2.6 on may laptop. However, even when I disable my laptop’s WiFi radio, the test program does not ever say “Device offline,” nor “Device error” nor “Device closed.” It seems to me that MQTT.js isn’t emitting any “problem” events, but I’m not sure how I could make it more disconnected
Update: I should add that the program keeps printing “Success” every second, even long after the WiFi is off. I know that MQTT with QoS 0 can optionally buffer outgoing messages, and it looks like the buffering is enabled in the Losant client (by way of letting MQTT.js use the default option). However, I’m not sure how long the queue will grow, and in any event it seems like I should still see the “offline” event.
Thanks!
Travis