Reputation: 23
As per the documentation of the Direct Method in the IoT Hub, it is stated that the user invokes a direct method through a service-facing URI "{iot hub}/twins/{device id}/methods/" and the device receives it on the MQTT topic "$iothub/methods/POST/{method name}/"
I have some confusion regarding how this mapping works.
Is there any internal mapping in the IoT hub that redirects the request to the specific device?
How do other devices identify that the method is not meant for them because the topic on which the device is listening does not seem to be device specific and we need only a particular device to receive that method invocation?
Upvotes: 2
Views: 4060
Reputation: 8255
Azure IoT Hub is a bidirectional integration gateway between a device and cloud (IoT Stack). Its primary goal is a fast ingestion of the device telemetry data to the cloud stream pipeline via a default (built-in) or custom endpoints.
Basically, the Azure IoT Hub has two kind of endpoints such as device-facing endpoints and service-facing endpoints. More details about these endpoints is here.
Internally, the Azure IoT Hub is not a bus oriented, it's not a generic MQTT Broker, it's not a generic communication broker, it is an integration gateway with a point-to-point star topology communications such as Device-To-Cloud (D2C) and Cloud-To-Device (C2D) messaging. There is no direct Device-To-Device messaging.
The C2D messaging has some limitations, see IoT Hub quotas and throttling. The C2D messaging can be in async or sync manner using a device twin features.
Basically, the C2D messaging is based on the point-to-point star communication, in other words you can not send a multicast broadcasting message to the devices. In this case, the recommend way is to create a job for sending a message to devices one by one.
Invoking a Direct Method on the device via the Azure IoT Hub is only via the service-facing endpoint using a Http protocol. On the other end such as a device-facing endpoint, it can be consumed only by device with a connection oriented protocol such as MQTT or AMQP.
The device-facing endpoint supports a MQTT v3.1.1 communication protocol, see more details here.
To evaluate behavior of the MQTT device with Azure IoT Hub can be done with 3rd party tools such as MQTTBox Client, https://iotdevtool.com, Azure IoT Hub Tester, etc.
The following screen snippets show invoking a Direct Method on the MQTT Device. Note the Azure IoT Hub Tester using a direct MQTT protocol with an Azure IoT Hub.
Step 1: Invoking a Direct Method on the service-facing endpoint (Http). Note, that the url address has a target device id (in this test Device10).
Step 2: The device (Device10) received a direct method
Step 3: The device (Device10) sent the response back to the invoker
Step 4: The invoker received a response from the Device10 within the timeout limit
Upvotes: 2
Reputation: 9710
Each Direct Method target a single device even if multi direct methods have the same name only the targeted device can receive the method.
So feel free to test and use it.
Upvotes: 0
Reputation: 2331
When a device authenticates with IoT Hub, it includes its device id. See: https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support#using-the-mqtt-protocol-directly
IoT Hub can then ensure devices receive the correct direct method invocations.
Upvotes: 0