Search code examples
node.jsazuremqttazure-iot-hubazure-iot-sdk

get messages from backend service app from device twins in microsoft azure


My goal is to update my service side app (in Node.js) when my azure device twin is updated or when I call the function. But I cant figure out how to do it. Is there a way to accomplish this with only the shared access primary key (in other words without the device key).

From my research I have found that this might work (but not sure)

Twin.get (){
}

The problem I have is I don't know how to complete this code. In this link I have given a screen shot of the function and its description. screenshot

Can anyone of you suggest a suitable code which might work(Doesn't have to be the one I mentioned)

Thanks in advance


Solution

  • Here is a sample how to do this:
    https://github.com/Azure/azure-iot-sdk-node/blob/master/service/samples/javascript/twin.js

    The relevant code is this:

    var Registry = require('azure-iothub').Registry;
    var registry = Registry.fromConnectionString(IOTHUB_CONNECTION_STRING);
    registry.getTwin(deviceId, function(err, twin) {
      if (err) {
        console.error(err.message);
      } else {
        console.log(JSON.stringify(twin, null, 2));
    }