Tutorial: Use namespace topics to route MQTT messages to Azure Event Hubs (Azure portal)
In this tutorial, you learn how to use a namespace topic to route data from MQTT clients to Azure Event Hubs. Here are the high-level steps:
Prerequisites
- If you don't have an Azure subscription, create an Azure free account before you begin.
- If you're new to Event Grid, read the Event Grid overview before you start this tutorial.
- Register the Event Grid resource provider according to the steps in Register the Event Grid resource provider.
- Make sure that port 8883 is open in your firewall. The sample in this tutorial uses the MQTT protocol, which communicates over port 8883. This port might be blocked in some corporate and educational network environments.
Create a namespace in the Azure portal
A namespace in Azure Event Grid is a logical container for one or more topics, clients, client groups, topic spaces and permission bindings. It provides a unique namespace, allowing you to have multiple resources in the same Azure region. With an Azure Event Grid namespace you can group now together related resources and manage them as a single unit in your Azure subscription.
Please follow the next sections to create, view and manage an Azure Event Grid namespace.
To create a namespace:
Sign-in to the Azure portal.
In the search box, enter Event Grid Namespaces and select Event Grid Namespaces from the results.
On the Event Grid Namespaces page, select + Create on the toolbar.
On the Basics page, follow these steps.
Select the Azure subscription in which you want to create the namespace.
Select an existing resource group or create a resource group.
Enter a name for the namespace.
Select the region or location where you want to create the namespace.
Select Review + create at the bottom of the page.
On the Review + create tab, review your settings and select Create.
On the Deployment succeeded page, select Go to resource to navigate to your namespace.
Create a namespace topic
If you aren't on the Event Grid Namespace page, follow the create, view, and manage namespaces steps to view the namespace you want to use to create the topic.
On the Event Grid Namespace page, select Topics option in the Event broker section on the left menu.
On the Topics page, select + Topic button on the command bar.
On the Create Topic page, type the name of the topic you want to create and select Create.
Enable managed identity for the Event Grid namespace
On the Event Grid Namespace page, select Identity under Settings on the left menu.
Turn on the switch to enable a system-assigned managed identity.
Select Save on the toolbar to save the setting.
Select Yes on the confirmation message.
Confirm that you see the object ID of the system-assigned managed identity and see a link to assign roles.
Check notifications in the portal to confirm that the managed identity is enabled for the namespace.
Enable MQTT broker for the Event Grid namespace
On the Event Grid Namespace page, select Configuration under Settings on the left menu.
Select the check box next to Enable MQTT broker.
Select Apply at the bottom of the page.
Check notifications in the portal to confirm that the MQTT broker is enabled for the namespace.
In a separate tab of the Web browser or in a separate window, use the Azure portal to create an Event Hubs namespace with an event hub.
Create an Event Hubs namespace
An Event Hubs namespace provides a unique scoping container, in which you create one or more event hubs. To create a namespace in your resource group using the portal, do the following actions:
In the Azure portal, select All services in the left menu, and select star (
*
) next to Event Hubs in the Analytics category. Confirm that Event Hubs is added to FAVORITES in the left navigational menu.Select Event Hubs under FAVORITES in the left navigational menu, and select Create on the toolbar.
On the Create namespace page, take the following steps:
Select the subscription in which you want to create the namespace.
Select the resource group you created in the previous step.
Enter a name for the namespace. The system immediately checks to see if the name is available.
Select a location for the namespace.
Choose Basic for the pricing tier. If you plan to use the namespace from Apache Kafka apps, use the Standard tier. The basic tier doesn't support Apache Kafka workloads. To learn about differences between tiers, see Quotas and limits, Event Hubs Premium, and Event Hubs Dedicated articles.
Leave the throughput units (for standard tier) or processing units (for premium tier) settings as it is. To learn about throughput units or processing units: Event Hubs scalability.
Select Review + Create at the bottom of the page.
On the Review + Create page, review the settings, and select Create. Wait for the deployment to complete.
On the Deployment page, select Go to resource to navigate to the page for your namespace.
Confirm that you see the Event Hubs Namespace page similar to the following example:
Create an event hub
To create an event hub within the namespace, do the following actions:
On the Overview page, select + Event hub on the command bar.
Type a name for your event hub, then select Review + create.
On the Review + create page, select Create.
You can check the status of the event hub creation in alerts. After the event hub is created, you see it in the list of event hubs.
Give Event Grid namespace the access to send events to the event hub
On the Event Hubs Namespace page, select Access control (IAM) on the left menu.
On the Access control page, select + Add on the command bar, and then select Add role assignment.
On the Add role assignment page, select Azure Event Hubs Data Sender from the list of roles, and then select Next at the bottom of the page.
On the Members page, follow these steps:
For the Assign access to field, select Managed identity.
Choose + Select members.
On the Select managed identities page, follow these steps:
Select your Azure subscription.
For Managed identity, select Event Grid Namespace.
Select the managed identity that has the same name as the Event Grid namespace.
Choose Select at the bottom of the page.
On the Add role assignment page, select Review + assign at the bottom of the page.
On the Review + assign page, select Review + assign.
Create an event subscription with Event Hubs as the endpoint
Switch to the tab of your Web browser window that has the Event Grid namespace open.
On the Event Grid Namespace page, select Topics on the left menu.
On the Topics page, select the namespace topic you created earlier.
On the Event Grid Namespace Topic page, select + Subscription on the command bar at the top.
On the Create Subscription page, follow these steps:
Enter a name for the event subscription.
For Delivery mode, select Push.
Confirm that Endpoint type is set to Event hub.
Select Configure an endpoint.
On the Select Event Hub, follow these steps:
Select the Azure subscription that has the event hub.
Select the resource group that has the event hub.
Select the Event Hubs namespace.
Select the event hub in the Event Hubs namespace.
Then, select Confirm selection.
Back on the Create Subscription page, select System Assigned for Managed identity type.
Select Create at the bottom of the page.
Configure routing in the Event Grid namespace
Navigate back to the Event Grid Namespace page by selecting the namespace in the Essentials section of the Event Grid Namespace Topic page or by selecting the namespace name in the breadcrumb menu at the top.
On the Event Grid Namespace page, select Routing on the left menu in the MQTT broker section.
On the Routing page, select Enable routing.
For Topic type, select Namespace topic.
For Topic, select the Event Grid namespace topic that you created where all MQTT messages will be routed.
Select Apply.
Check notifications to confirm that the namespace is enabled with the routing information.
Create clients, topic space, and permission bindings
Follow steps in the quickstart: Publish and subscribe on an MQTT topic to:
Create a client. You can create the second client if you want to, but it's optional.
Create a topic space.
Create publisher and subscriber permission bindings.
Use MQTTX to send a few messages.
Verify that the event hub received those messages on the Overview page for your Event Hubs namespace.
View routed MQTT messages in Event Hubs by using a Stream Analytics query
Navigate to the Event Hubs instance (event hub) within your event subscription in the Azure portal. Process data from your event hub by using Stream Analytics. For more information, see Process data from Azure Event Hubs using Stream Analytics - Azure Event Hubs | Microsoft Learn. You can see the MQTT messages in the query.
Next steps
For code samples, go to this GitHub repository.