How to configure Azure IoT Hub in Azure Portal to loopback message data for "Device-to-Cloud" back to the device.

Mike 21 Reputation points

I am trying to loopback(echo) message data, that cloud receive from the device back to the device.
for now, the device can publish to cloud(Device-to-Cloud) properly, checked by Azure IoT Explorer(Telemetry function)
and the cloud can publish to device(Cloud-to-Device) properly, by "Message to device" feature in Azure Portal.
But I can not see the the feature to loopback message in the "Message to device" feature.

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,136 questions
{count} votes

2 answers

Sort by: Most helpful
  1. chbeier 1,866 Reputation points

    Hello @mikesaran,
    IoT Hub has no built-in function to loopback a received message. Saying this, it is quite easy to build with Azure Functions. Please check the docs at functions-bindings-event-iot to learn how to react in your Azure Function on a received IoT Hub message, process it and send it to its destination, in your case I believe ingesting it to the Cloud2Device queue of the originating sender.

    Here is some code I wrote years ago to simply measure duration from sending the message on the device until it was consumed by the Azure Function. The Function calls a direct method on the device to signal it has processed the message.
    Be aware that I haven't tested it recently and it is likely not to work, but with the documentation it should be a good starting point.

    var Client = require('azure-iothub').Client;  
    // uses azure function app parameter  
    var connectionString = process.env.AzureIoTHubConnectionString;  
    var methodName = 'doCallback';  
    // default receiver  
    var deviceId = 'ABC-4711-123';  
    module.exports = function (context, myEventHubTrigger) {  
        var recvTs = new Date().getTime();  
        var jsonMessage = myEventHubTrigger;  
        context.log("Received raw message from device at " + recvTs + " content: " + JSON.stringify(myEventHubTrigger));  
        context.log("time Diff: " + (recvTs - jsonMessage.payload));  
        context.log("parsed message:");  
        context.log("    receiver : " + jsonMessage.receiver);  
        context.log("    payload : " + jsonMessage.payload);  
        if (jsonMessage.receiver != null) {  
            var deviceId = jsonMessage.receiver;  
        var client = Client.fromConnectionString(connectionString);  
        var methodParams = {  
            methodName: methodName,  
            payload:  JSON.stringify({"devSendTs" : jsonMessage.payload, "funcRecvTs": recvTs}),  
            responseTimeoutInSeconds: 5  
        context.log("invoke Method on: " + deviceId);  
        client.invokeDeviceMethod(deviceId, methodParams, function (err, result) {  
            if (err) {  
                console.error('Failed to invoke method \'' + methodName + '\': ' + err.message);  
            } else {  
                console.log(methodName + ' on ' + deviceId + ':');  
                console.log(JSON.stringify(result, null, 2));  
    1 person found this answer helpful.
    0 comments No comments

  2. QuantumCache 20,031 Reputation points

    Hello @mikesaran,

    Thanks for posting this interesting question, below information would help you understand the basic purpose\capabilities of Azure IoTHub.
    I would highly recommend these topics before we design our IoT system, and a lot of helpful documentation + community support are available to guide you!

    Loopback scenario: I think you wanted to return the message to the producer(IoTDevice). You may make use of a backend application (Azure Functions in this example) that can send the received messages to the IoT devices via Azure IoT hub, please have a look at the below architecture example.

    First route all the messages to the backend application's endpoint via IoTHub, then the backend application will receive the messages and post them back to Azure IoTHub which will route to the destination IoT Devices.

    Instead of route messages from IoT Hub, you may be interested in Azure IoT Hub trigger for Azure Functions

    Device-to-cloud communications guidance (D2C)
    When sending information from the device app to the solution back end, IoT Hub exposes three options:

    Cloud-to-device communications guidance (C2D)
    IoT Hub provides three options for device apps to expose functionality to a back-end app:


    Use IoT Hub message routing to send device-to-cloud messages to different endpoints

    Understand and use device twins in IoT Hub
    Device twins store device-related information

    Understand and use module twins in IoT Hub
    On the device side, the IoT Hub device SDKs enable you to create modules where each one opens an independent connection to IoT Hub.
    Module identity and module twin provide the same capabilities as device identity and device twin but at a finer granularity.

    Please comment in the below section if you need further help in this matter!

    0 comments No comments