Azure IoT Hub C2D Message Queues Logging into storage/queue service.

Dhanavath Vishnu 366 Reputation points
2024-02-15T15:35:54.8566667+00:00

Hi Team, Hello @LeelaRajeshSayana-MSFT ,We are having an Azure IoT Edge project, where we have multiple IoT Edge devices connected to an IoT HUB. When we send the cloud to devices messages from our backend application to edge devices, the C2D messages are getting accumulated in the queue, even though some of those messages are consumed by edge devices. This behaviour is different for different edge devices, so my idea is it possible to log those C2D queue message to any storage service for viewing to compare the messages which are delivered to edge devices, and which are not. If solution available, please do suggest. Thanks & Regards D. Vishnu

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,134 questions
0 comments No comments
{count} votes

Accepted answer
  1. LeelaRajeshSayana-MSFT 13,791 Reputation points
    2024-02-16T02:21:49.8066667+00:00

    Hi @Dhanavath Vishnu Greetings! Thank you for posting the question here.

    All cloud-to-device messages get queued in per-device queues to guarantee at-least-once message delivery. Once the devices send an acknowledgment message to the IoT Hub indicating that the message reception is complete, then the IoT Hub removes the messages from the queue. If the device fails to receive the message, they end up either in the Dead lettered state or Enqueued state based on why the message reception by the device failed. Please find the below image to understand how the cloud to device message live cycle works.

    Diagram showing the life-cycle state graph of cloud-to-device messages.

    You can get more information on this from the article section - The cloud-to-device message life cycle

    We can access these status state of the message by using the Feedback receiver method provided on the SDK. The Receive delivery feedback section of the article provides the implementation of this method and how to use it to receive the message received acknowledgement status from the device. Below is the method for your reference.

    private async static void ReceiveFeedbackAsync()
    {
         var feedbackReceiver = serviceClient.GetFeedbackReceiver();
    
         Console.WriteLine("\nReceiving c2d feedback from service");
         while (true)
         {
             var feedbackBatch = await feedbackReceiver.ReceiveAsync();
             if (feedbackBatch == null) continue;
    
             Console.ForegroundColor = ConsoleColor.Yellow;
             Console.WriteLine("Received feedback: {0}",
               string.Join(", ", feedbackBatch.Records.Select(f => f.StatusCode)));
             Console.ResetColor();
    
             await feedbackReceiver.CompleteAsync(feedbackBatch);
         }
     }
    

    Based on the status code returned from the messages, you can use this approach to differentiate between the messages that get delivered and those that stay in the queue. You can store the messages filtered based on this conditioning and store it at any end point of your choice for later comparision.

    Hope this helps! Please let us know if you have any additional questions.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Sander van de Velde 29,461 Reputation points MVP
    2024-02-16T18:25:25.1466667+00:00

    Hello @Dhanavath Vishnu ,

    welcome to this moderated Azure community forum.

    The answer from @LeelaRajeshSayana-MSFT is spot on regarding the cloud messaging.

    Regarding logging cloud messages, please take a look at Operations monitoring:

    Allows users to monitor the status of operations on their IoT Hub real-time, across four categories: device identity operations, device telemetry, cloud-to-device commands, and connections.

    If you add an IoT Hub input in Azure Stream Analytics, you can monitor this:

    enter image description here

    Please share if this ASA feature helps you.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.