Search code examples
mqttazure-iot-hub

Can I send a message to a group of Azure IoT Hub Devices connected via MQTT WebSocket?


Goal: Send messages to a group of Azure IoT Hub Devices that are connected via MQTT WebSocket.

Initial Idea: Have a group of IoT Hub Devices (group X) subscribe to messages with topic X.

Problem:

On the Communicate with your IoT hub using the MQTT protocol docs, I don't see a way to subscribe a device to additional topics. I only see examples of a subscription to a device specific endpoint devices/{device_id}/messages/devicebound/#.

The page goes on to say "IoT Hub is not a general purpose pub-sub messaging broker, it only supports the documented topic names and topic filters." I think those topics are

  • devices/{device_id}/messages/devicebound/
  • devices/{device_id}/messages/devicebound/#

My Conclusion: I can't have a group of IoT Hub Devices (group X) subscribe to messages with topic X.

Questions:

  1. Is my conclusion correct?
  2. If my conclusion is correct, is there another way I can send messages to a group of Azure IoT Hub Devices that are connected via MQTT WebSocket?

Solution

  • Your conclusion is correct. Azure IoT Hub is not an MQTT broker per se. If you want to send messages to a group of devices connected to Azure IoT Hub (independently of the protocol they are connected with), you need to look into one of the Cloud 2 Device features of IoT Hub. Depending on your scenario you can use one of the following: Cloud 2 Device messages, Twins or Methods. IoT Hub exposes APIs on the back-end side (easily used with the Service Client SDKs) allowing to use any of those.

    C2D messages are used to send raw data to devices, meaning you implement your own "protocol" between cloud and device. C2D messages are also persistent in the Cloud as IoT Hub will retain them (for a certain time defined in settings) if the device is not connected.

    Device Twins allows to synchronize a configuration of a device with the Cloud and allows for batch operations on devices (certainly one you want to look closer into).

    Device Direct Methods allows to call a function running on a device from the cloud and like Twins supports batch operations.