How to send different message to different topics with same Mqtt Connection

Pisipati, Santosh 0 Reputation points

Hi Team,

we are working on one of the project where we wanted data to publish using Mqtt Protocol.
though we are able to publish message to single topic(devices/{deviceId}/messages/events/), we wanted to check if we can be able to publish message to other topic (devices/Device1/messages/events/#) using the same mqtt client connection.

we are using SAS token and paho mqtt client to connect and publish data to Azure IoT.

Your help in this regards will be much helpful.



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

2 answers

Sort by: Most helpful
  1. Dom 1,556 Reputation points Microsoft Employee

    Azure IoT Hub isn't a fully compliant MQTT broker, so the only supported topic for sending device-to-cloud messages is devices/{deviceId}/messages/events/.

    If you want to use a fully compliant MQTT broker in Azure where you have full control over the topics you use, use the MQTT broker in Azure Event Grid:

    0 comments No comments

  2. Sander van de Velde 29,461 Reputation points MVP

    Hello @Pisipati, Santosh,

    welcome to this moderated Azure community forum.

    The Azure IoT Hub is a popular IoT Cloud gateway due to the strong security, flexible routing and wide support for devices and SDKs.

    The IoT Hub uses MQTT (amongst others) as a transport protocol.

    The MQTT topics it supports is tightly related to the DeviceTwin capabilities as seen in this post.

    This is also a restriction, the IoT Hub only offers support for these (few) MQTT topics. Other topics are ignored.

    In the past, Microsoft has attempted to overcome this limitation with additional (MQTT5) support.

    It is not recommended to go this path.

    So what now?

    Last year, Microsoft has introduced full (vanilla) MQTT support in the EventGrid Namespace (comparison).

    This will offer exactly what you need, secure Device communication over MQTT, check out this blog post for a walk through.

    Notice you need to secure your devices with eg. X509 certificates as the Identity. If you use a constrained device without a certificate store, you also need a Digicert certificate for the TLS connection as seen in this demonstration.

    Check this documentation about client authentication, including Entra ID authentication.

    A sample Python client is available on GitHub.

    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