0

I am not exactly starting out with device twin and azure but closer to newbie than expert. I cannot figure out how to correctly manage connection problems with the device twins in azure.

I have a long running node application that opens a client. I find that after days or weeks it may start failing to communicate with the backed device twin. I am not sure how to manage this or more specifically, trap the problem and reconnect.

This is the code:

client.getTwin(function(err, twin) {
 if (err) {
     console.error('could not get twin');
 } else {
     console.log('retrieved device twin');       
     twin.on('properties.desired', function(desiredChange) {
        console.log('Do something');
     }
}

}

Is there a twin.error method?

Part of the problem is that I cannot seem understand/find the correct documentation on these methods.

mg360
  • 103
  • 2
  • 10
  • First, find the exact error information using `console.log('Could not get twin: ' + err);`. And when the issue occurs you can install and run [iothub-diagnostics](https://github.com/Azure/iothub-diagnostics) to check the connection. Related issue [1](https://github.com/Azure/azure-iot-sdk-node/issues/34). – Rita Han Apr 05 '18 at 01:54
  • This is more about what to do about it if it was in the field. To be specific though the last message I was getting was a timeoutError but I had some sort of connection because Azure was processing direct messages. – mg360 Apr 07 '18 at 03:45
  • Is this issue could reproduced every time? If this issue occur, can you get the device twin using other SDK from client side? And here is a helpful link to handle [connectivity and retries](https://github.com/Azure/azure-iot-sdk-node/wiki/Connectivity-and-Retries) for the azure-iot-sdk-node. – Fei Xue Apr 16 '18 at 01:44

1 Answers1

0

It seems there was a a known bug. However this code really helped me:

Here is the link and the code below: https://github.com/Azure/azure-iot-sdk-node/issues/28

'use strict'

var token = "...";
var Client = require('azure-iot-device').Client;
var DeviceProtocol = require('azure-iot-device-mqtt').MqttWs; // Default transport for Receiver
var client = Client.fromSharedAccessSignature(token, DeviceProtocol);
var isDissconnected = true;

client.on('disconnect', function () {
    isDissconnected = true;
    console.log('Disconnected');
    client.close(function () {
        console.log('Closed');
    });
});
client.on('connected', function () {
    console.log('Connected');
});
client.on('error', function (err) {
    console.error('ERROR: ' + err.message);
});

var connectClient = function () {
    console.log('Connecting client');
    client.open(function (err, transport) {
        if (err) {
            console.log('Could not connect: ' + err);
        }
        else {
            isDissconnected = false;
            client.getTwin(function (err, twin) {
                if (err) {
                    console.log('Could not get twin: ' + err);
                }
                else {
                    console.log('Got twin');
                }
            });
        }
    });
}
connectClient();
setInterval(function () {
    if (isDissconnected) {
        console.log("Retrying to connect...");
        connectClient();
    }
}, 10000);
mg360
  • 103
  • 2
  • 10