IoTHubDeviceClient: Is there any danger in calling patch_twin_reported_properties() from inside on_message_received coroutine?

Steven Drake 0 Reputation points
2023-01-25T01:34:12.07+00:00

I am using the IoTHubDeviceClient from the Azure SDK for Python, and have assigned a coroutine to IoTHubDeviceClient.on_message_received.

When a cloud-to-device message is received and my on_message_received handler is called, it may modify the state of the device twin properties. I want to immediately send that change to the cloud by calling IoTHubDeviceClient.patch_twin_reported_properties().

My concern is whether it is "safe" to call patch_twin_reported_properties() from within the context of the on_message_received coroutine.

I added this call and in testing it works just fine, but I am wondering if there are any dangers in doing this.

Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
207 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sander van de Velde 28,161 Reputation points MVP
    2023-01-25T23:31:53.5833333+00:00

    Hello @Steven Drake ,

    the IoTHubDeviceClient.on_message_received is used to receive cloud messages.

    Cloud messages are stored in a queue on the IoT Hub as long as the device is not connected.

    Once the device connects the messages in the queue are then sent to the device.

    The Cloud messages mechanism only supports sending messages to the device, there is no response mechanism added. It's like 'fire and forget' but asynchronous.

    The proper way to respond is to make use of the reported properties part of the DeviceTwin and put a response value in there.

    So you are good to go.

    The only thing you need to know is that reported property updates are charged.


    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.

    0 comments No comments