Search code examples
azureazure-functionsazure-iot-hub

Why is IoTHubMessages an array in Azure Functions


I have messages from Azure IoT hub and I context.log the bindings of IoTHubMessages. It shows object.

enter image description here

The code in the image above is working.As you can see the console when the function is triggered, It log the IoTHubMessages as an object. But the code still works when I loop them. How is that happening?


Solution

  • To elaborate on Hury Shen's comment: When you create a new Azure Functions App in JavaScript (through VSCode), the function.json will automatically have cardinality set to many. This will allow messages to be batched, which is why your input parameter is an array. If you don't get a lot of messages, you will see arrays with only 1 value being passed into your Function.

    If you change many to one:

    "cardinality": "one",
    

    Then messages will never be batched and your input parameter will be a single message, even when there are a lot of messages coming in. There is a Microsoft doc that mentions this configuration.