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));
}
});
context.done();
};