send mqtt data from IoT Hub to VM RRS feed

  • Question

  • I want to send my received sensor data from IoT Hub to my VM (hosting mqtt broker and subscriber) using mqtt client. 

    Is there a service or library available in IoT Hub to support this?

    Thank you. 

    Thursday, August 3, 2017 9:36 PM

All replies

  • hello

    I have moved your thread since this forum is for Windows IoT and it is not so appropriate for your thread to be here.


    Thursday, August 3, 2017 9:48 PM
  • There are various ways to implement such kind of scenario.

    For example, your devices will have to send telemetry data IoTHub endpoints. You can use any of supported protocols like MQTT.

    However IotHub simplifies messaging, security etc. Because of this you are not required to implement broker on your VM. You should implement Cloud to Device messaging scenario on VM to be able to receive messages.

    Following URL describes all scenarios.


    Here is described how to read messages: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messages-read-builtin

    Code example:


    Damir Dobric

    Monday, August 7, 2017 6:47 AM
  • Hi @BenNguyen,

    - In addition to Damir's answer, there is no built-in a service/library for forwarding a raw mqtt message from the device to the generic MQTT Broker via the Azure IoT Hub. This kind of scenario can be achieved with an Azure Function and 3rd party MQTT Client Library, for example M2Mqtt.

    The following screen snippet shows an example of this solution:

    The code snippet of the precompiled EventHub Trigger Azure Function example is:

    using Microsoft.Azure.WebJobs.Host;
    using Microsoft.ServiceBus.Messaging;
    using Newtonsoft.Json;
    using System.Text;
    using System.Threading.Tasks;
    using uPLibrary.Networking.M2Mqtt;
    namespace RKiss.AFNLib
        public static class IoTHubMqttBridge
            static MqttClient client = null;
            static IoTHubMqttBridge()
                client = new MqttClient("broker.mqttdashboard.com", 1883, false, null, null, MqttSslProtocols.None, null);
            public static async Task Publish(EventData ed, TraceWriter log, Microsoft.Azure.WebJobs.ExecutionContext executionContext)
                log.Info($"[{executionContext.InvocationId}] {executionContext.FunctionName} trigger function Publish a message: {ed.SequenceNumber}");
                if (client != null)
                    if (!client.IsConnected)
                        log.Info($"[{executionContext.InvocationId}] Device Bridge1 connected through {client.ClientId}");
                    string deviceId = ed.SystemProperties.ContainsKey("iothub-connection-device-id") ? ed.SystemProperties["iothub-connection-device-id"].ToString() : "Test";
                    await Task.Run(()=>client.Publish($"Bridge1/events/{deviceId}/", ed.GetBytes()));
                    log.Info($"[{executionContext.InvocationId}] client = null");

    and the bindings:

      "bindings": [
          "type": "eventHubTrigger",
          "name": "ed",
          "direction": "in",
          "path": "rk20170721-iot",
          "connection": "rk20170721-iot_events_IOTHUB",
          "consumerGroup": "Bridge",
          "cardinality": "many"
      "scriptFile": "..\\bin\\IoTHubMqttBridge.dll",
      "entryPoint": "RKiss.AFNLib.IoTHubMqttBridge.Publish",
      "disabled": false

    The above AF has been tested with HIVEMQ Broker, but it can be use for any generic MQTT Broker.

    The following picture shows a HIVEMQ Client:



    • Edited by Roman Kiss Tuesday, August 8, 2017 8:08 PM
    Tuesday, August 8, 2017 4:07 PM