Tutorial: Route MQTT messages to Azure Event Hubs from Azure Event Grid with Azure portal

Use message routing in Azure Event Grid to send data from your MQTT clients to Azure services such as storage queues, and Event Hubs. In this tutorial, you perform the following tasks:

  • Create Event Subscription in your Event Grid topic.
  • Configure routing in your Event Grid Namespace.
  • View the MQTT messages in the Event Hubs using Azure Stream Analytics.

Prerequisites

  • If you don't have an Azure subscription, create an Azure free account before you begin.
  • If you're new to Azure Event Grid, read through Event Grid overview before starting this tutorial.
  • Register the Event Grid resource provider as per Register the Event Grid resource provider.
  • Make sure that port 8883 is open in your firewall. The sample in this tutorial uses MQTT protocol, which communicates over port 8883. This port might be blocked in some corporate and educational network environment.
  • An Event Grid Namespace in your Azure subscription. If you don't have a namespace yet, you can follow the steps in Publish and subscribe on a MQTT topic.
  • This tutorial uses Event Hubs, Event Grid custom topic, and Event Subscriptions. You can find more information here:
  • Creating an Event Grid topic: Create a custom topic using portal. While creating the Event Grid topic, ensure to create with Event Schema as Cloud Event Schema v1.0 in the Advanced tab.

Screenshot showing Event Grid topic create flow Advanced tab.

Create Event Subscription with Event Hubs as endpoint

  1. In your Event Grid topic, go to Event Subscriptions page under Entities in left rail.
  2. Select + Event Subscription to add new subscription.

Screenshot showing the Add Subscription button in the Event Grid topic overview page.

  1. For Event Schema, select Cloud Event Schema v1.0.
  2. Add Event Hubs as Endpoint Type, and provide your event hub information for endpoint, and select Confirm Selection button.

Screenshot showing the event subscription create flow and configuration.

  1. Then Create the Event subscription.

Configure routing in the Event Grid Namespace

  1. Navigate to Routing page in your Event Grid Namespace. Screenshot showing the Event Grid routing configuration page to enable routing for the namespace.

  2. Select Enable routing

  3. Select 'Custom topic' option for Topic type

  4. Under Topic, select the Event Grid topic that you have created where all MQTT messages will be routed.

Screenshot showing the routing configuration through the portal.

  1. Select Apply

Note

Routing Event Grid topic considerations: The Event Grid topic that is used for routing has to fulfill following requirements:

  • It needs to be set to use the Cloud Event Schema v1.0.
  • It needs to be in the same region as the namespace.
  • You need to assign "EventGrid Data Sender" role to yourself on the Event Grid Topic.

Configuration

  • Go to the Event Grid topic resource.
  • In the "Access control (IAM)" menu item, select Add role assignment.
  • In the "Role" tab, select "EventGrid Data Sender", then select Next.
  • In the "Members" tab, click on + Select members, then type your AD user name in the "Select" box that will appear (e.g. user@contoso.com).
  • Select your AD user name, then select Review + assign.

Viewing the routed MQTT messages in Azure Event Hubs using Azure Stream Analytics query

  1. After configuring the routing for the namespace, publish MQTT messages among the clients (as described in the article Publish and subscribe on an MQTT topic.)

  2. Navigate to the Event Hubs instance within your Event Subscription on Azure portal. Process data from your event hub using Azure Stream Analytics. (Process data from Event Hubs Azure using Stream Analytics - Azure Event Hubs | Microsoft Learn) You can see the MQTT messages in the query.

Screenshot showing the MQTT messages data in Event Hubs using Azure Stream Analytics query tool.

Next steps