How to receive device connection events from Azure IoT Hub? - azure-iot-hub

Is it possible to get notified that a device has connected to IoT hub without polling?
The only option I've found is to poll RegisteryManager.GetDevicesAsync() and loop through the registered devices to see if they are connected or not. I could poll, however I have no idea what sort of throttling limits exist.
That code looks like:
var devices = await registryManager.GetDevicesAsync(maxCountOfDevices);
if (devices != null)
{
foreach (var device in devices)
{
// do something with the connection state like
// notify services
device.ConnectionState
}
}
What I'd like is to be able to get all registered devices, keep them in memory, and just listen for connection events.

Microsoft Azure IoT team suggests implementing the heartbeat pattern because connectionState is not accurate:
https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-identity-registry#device-heartbeat

I Tried it with operation monitoring in Azure IOT Hub. It provide an event hub endpoint to read the verbose operation. So here i find the event of connection and disconnection of IOT device to IOT hub. But now the issue is device messages and connection/disconnection events are async.

Related

Disconnect (or Close) device connection if no message receive for a duration

In my application, we have devices which connects with Azure IoTHub. Is there a way in IoTHub which enforce device disconnection if no message received from a device for a duration of 15 minutes ? Is there any such functionality present in Azure IotHub out of the box ?
Unfortunately there is no such feature available in Azure IoTHub.
You can manage it programmatically. You can keep the device in sleep mode when there is no action on device side.
You can post a feature request here.
if you're using python, you can use get_invoke_cli package to call cli
az iot hub device-identity update --resource-group {resource-group} --hub-name {Hub} --device-id {device id} --sta disabled
to disable your iotdevice to connect to iothub

Where can I find azure IoT device messages?

I have sent messages to Azure IoT Hub device called dev1, I could not see the messages in IoT Hub but, I can read the messages only when the client application is online when the sender is sending messages. Azure IoT Hub supports only online messaging and no offline messaging? If offline message support is there, where are these messages are stored, I couldn't see the messages in IoT Hub.
When I configure the custom endpoint as Blob storage, I can see messages are stored in blobs.
Please help me on this.
Thanks in advance
If I understand correctly you are looking for reading the messages directly on IoT Hub portal UI. If that is the case, then one of the things which you can make sure about D2C Messages in IoT Hub portal (UI perspective) is looking at the Metrics chart (See below Images). For reading the actual payload you have to make use of in-built Event Hub endpoint or routing to other supported endpoints.(You have already mentioned in your scenario-Client/Sender applications, So I think you have already known this method of reading messages)
The Metrics chart atleast tells you that the messages are received in IoT Hub (UI), you can't read them on the Portal(UI).
IoT Hub is built on top of Event Hubs, and that's where your messages will be until you start reading them. They will be stored there for 1 day by default, although you can change that up to 7 days. For more information on retention, please read this page.

IOT-HUB device node.js sdk message.on method is not working

Once the Event will be sent to Cloud IOT-HUB from Device thru Device Node.JS sdk, once message is received on IOT-Hub same needs to be acknowledged to nodejs.
We found couple of reference regarding the same and the below mentioned method should work for the same.
client.on('message', function(msg) {
console.log('Received Message from IoT Hub: ' + msg.data);
client.complete(msg, printResultFor('completed'));
// We are done here
});
But this method is not triggering at all in any circumstance.
Please suggest.
Your description of the question and your code lines are inconsistent.
Once the Event will be sent to Cloud IOT-HUB from Device thru Device
Node.JS sdk, once message is received on IOT-Hub same needs to be
acknowledged to nodejs.
It is talking about D2C messages received by IoT Hub.
client.on('message', function(msg) {
console.log('Received Message from IoT Hub: ' + msg.data);
client.complete(msg, printResultFor('completed'));
// We are done here
});
But it is talking about receiving C2D messages on device client end.
To trigger this method client.on you need send a C2D message from IoT Hub server end. For testing purpose, you can simply send a C2D message either from Azure Portal or IoT Hub Device Explorer tool:
If you run this sample: simple_sample_device.js you will receive the D2C message like this(I add this line console.log('client.on triggered!!!'); to the sample code):

Receiving acknowledgements using IoT Hub

When using DeviceClient I can send messages using SendEvent and files using SendBlob. But I did not find a way to receive acknowledgement that messages/files have been received by Azure IoT Hub?
The only way I found to solve this is using serviceClient.GetFileNotificationReceiver().
Am I missing something or is this the only way?
Also it seems I need SharedAccessKeyName to use ServiceClient. But this is not present in e.g. tokens created by DeviceExplorer (which I use for DeviceClient). Any advice is appreciated.
For Java and C sdks there are IotHubEventCallback and IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK but for C# there is no such interface implemented.
So, for C#, a message will be sent successfully if DeviceClient.SendEventAsync() without throwing any exception, otherwise it fails.
Or you can utilize Event Hub-compatible endpoint to monitor the status of operations on your IoT hub, D2C message, file upload...
For ServiceClient, you need Azure IoT Hub connection string, not device connection string. You can find it in Configuration of Device Explorer:

Azure IotHub doesn't receiving any messages

I'm just set up my iot hub that should receiving messages from my test device. It work's perfect about one day. On second day when I send messages to this azure service I don't getting any exception but my iot hub doesn't receving any messages. I don't know what is the main problem of that ? Additionaly I have one worker role that handle all messages on event hub processor from this iot hub. Maybe is there any extra settings that stops my iot hub or something more ? I can't find any information on other sites maybe someone have similiar problem ?
As per my understanding,
Your device sends message to IOT Hub
Your worker role reads message from this IOT hub using EHP
If this is the case and you are not receiving messages at EHP than CONSUMER GROUP could be the issue. Basically, IOT hub serves messages per consumer group. By default each IOT Hub has $Default group. If some other service is already reading message from this consumer group than you won't be able to get message.
You can create another consumer group for your worker role. IOT Hub serves same message to each consumer group.