Event Grid mqtt topic filtering for Azure functions

Thomas Jackson 0 Reputation points
2024-12-17T15:58:34.0866667+00:00

Hi all,

I am using Event Grid as an MQTT broker to receive data from multiple MQTT devices, each publishing to unique topics (e.g., "Devices/DeviceType1/Device1", "Devices/DeviceType2/Device100"). I aim to dynamically route messages from a specific mqtt topic to a specific function, rather than having a single topic trigger all functions.

Currently, all functions subscribed to Event Grid are triggered when any message arrives in the event grid broker. Is it possible to implement topic-based filtering within Event Grid to direct messages from specific MQTT topics to their designated functions? If this is not achievable, is there any alternatives that could do this?

Thanks in advance for any help.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,250 questions
Azure Event Grid
Azure Event Grid
An Azure event routing service designed for high availability, consistent performance, and dynamic scale.
407 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Khadeer Ali 1,400 Reputation points Microsoft Vendor
    2024-12-17T18:24:46.1666667+00:00

    @Thomas Jackson

    Welcome to the Microsoft Q&A Platform! Thank you for reaching out regarding your query on Event Grid mqtt topic filtering for Azure functions

    Yes, it is possible to implement topic-based filtering within Event Grid to direct messages from specific MQTT topics to their designated functions. You can use the "Advanced Filters" feature in Event Grid to filter events based on the MQTT topic.

    Here's how you can set up topic-based filtering in Event Grid:

    1. Create a new subscription for each function that you want to trigger based on a specific MQTT topic. When creating the subscription, specify the MQTT topic as the "Subject Begins With" filter.
    2. In your Azure Function code, use the "EventGridTrigger" attribute to specify the subscription name that corresponds to the MQTT topic you want to handle. For example:
    public static void Run([EventGridTrigger] EventGridEvent eventGridEvent, ILogger log) {
     // Handle the event based on the MQTT topic 
    }
    

    In this example, the function will only be triggered for events that match the subscription with the specified name.

    1. Publish MQTT messages to the appropriate topics. Only the functions that have a subscription with a matching "Subject Begins With" filter will be triggered.

    Note that the "Subject Begins With" filter is a prefix match, so you can use wildcards to match multiple topics. For example, if you want to trigger a function for all MQTT topics that start with "Devices/DeviceType1", you can set the "Subject Begins With" filter to "Devices/DeviceType1/*".

    I hope this helps! Let me know if you have any further questions.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.