You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/event-grid/mqtt-publish-and-subscribe-cli.md
+36-33Lines changed: 36 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,21 @@
1
1
---
2
-
title: 'Quickstart: Publish and subscribe on an MQTT topic by using the CLI'
2
+
title: 'Quickstart: Publish on an MQTT topic by using the CLI'
3
3
description: Quickstart guide to use the Azure Event Grid MQTT broker feature and the Azure CLI to publish and subscribe to MQTT messages on a topic.
4
4
ms.topic: quickstart
5
5
ms.custom:
6
6
- build-2023
7
7
- devx-track-azurecli
8
8
- ignite-2023
9
-
ms.date: 11/15/2023
9
+
ms.date: 06/24/2025
10
10
author: george-guirguis
11
11
ms.author: geguirgu
12
12
ms.subservice: mqtt
13
+
#customer intent: As a developer, I want to use the Azure Event Grid MQTT broker for messaging of apps and IoT.
13
14
---
14
15
15
16
# Quickstart: Publish and subscribe to MQTT messages on an Event Grid namespace with the Azure CLI
16
17
17
-
The Azure Event Grid MQTT broker feature supports messaging by using the MQTT protocol. Clients (both devices and cloud applications) can publish and subscribe to MQTT messages over flexible hierarchical topics for scenarios such as high-scale broadcast and command and control.
18
+
In this quickstart, you use the Azure Event Grid MQTT broker feature to support messaging by using the MQTT protocol. Clients, including both devices and cloud applications, can publish and subscribe to MQTT messages over flexible hierarchical topics for scenarios such as high-scale broadcast and command and control.
18
19
19
20
In this article, you use the Azure CLI to:
20
21
@@ -27,92 +28,93 @@ If you don't have an [Azure subscription](/azure/guides/developer/azure-develope
27
28
28
29
## Prerequisites
29
30
30
-
- If you're new to Event Grid, read through the [Event Grid overview](../event-grid/overview.md) before you start this tutorial.
31
+
- If you're new to Event Grid, see the [Event Grid overview](../event-grid/overview.md) before you start this tutorial.
31
32
- Register the Event Grid resource provider according to the steps in [Register the Event Grid resource provider](../event-grid/custom-event-quickstart-portal.md#register-the-event-grid-resource-provider).
32
-
- 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.
33
-
- Use the Bash environment in [Azure Cloud Shell](../cloud-shell/overview.md). For more information, see [Quickstart for Bash in Azure Cloud Shell](../cloud-shell/quickstart.md).
34
-
- If you prefer to run CLI reference commands locally, [install](/cli/azure/install-azure-cli) the Azure CLI. If you're running on Windows or macOS, consider running the Azure CLI in a Docker container. For more information, see [Run the Azure CLI in a Docker container](/cli/azure/run-azure-cli-docker).
35
-
- If you're using a local installation, sign in to the Azure CLI by using the [`az login`](/cli/azure/reference-index#az-login) command. To finish the authentication process, follow the steps that appear in your terminal. For other sign-in options, see [Sign in with the Azure CLI](/cli/azure/authenticate-azure-cli).
36
-
- When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see [Use extensions with the Azure CLI](/cli/azure/azure-cli-extensions-overview).
33
+
- Make sure that port 8883 is open in your firewall. This tutorial uses the MQTT protocol, which communicates over port 8883. This port might be blocked in some corporate and educational network environments.
34
+
- Use the Bash environment in [Azure Cloud Shell](../cloud-shell/overview.md). For more information, see [Get started with Azure Cloud Shell](../cloud-shell/quickstart.md).
35
+
- If you prefer to run CLI reference commands locally, [install](/cli/azure/install-azure-cli) the Azure CLI. If you run on Windows or macOS, consider running the Azure CLI in a Docker container. For more information, see [Run the Azure CLI in a Docker container](/cli/azure/run-azure-cli-docker).
36
+
- If you're using a local installation, sign in to the Azure CLI by using the [`az login`](/cli/azure/reference-index#az-login) command. To finish the authentication process, follow the steps that appear in your terminal. For other sign-in options, see [Authenticate to Azure using Azure CLI](/cli/azure/authenticate-azure-cli).
37
+
- When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see [Manage Azure CLI Extensions](/cli/azure/azure-cli-extensions-overview).
37
38
- Run [az version](/cli/azure/reference-index?#az-version) to find the version and dependent libraries that are installed. To upgrade to the latest version, run [az upgrade](/cli/azure/reference-index?#az-upgrade).
38
39
- This article requires version 2.53.1 or later of the Azure CLI. If you're using Azure Cloud Shell, the latest version is already installed.
39
40
- You need an X.509 client certificate to generate the thumbprint and authenticate the client connection.
40
-
- Review the Event Grid namespace [CLI documentation](/cli/azure/eventgrid/namespace).
41
+
- Review the Event Grid namespace [CLI commands](/cli/azure/eventgrid/namespace).
41
42
42
43
## Generate a sample client certificate and thumbprint
43
44
44
45
If you don't already have a certificate, you can create a sample certificate by using the [step CLI](https://smallstep.com/docs/step-cli/installation/). Consider installing manually for Windows.
45
46
46
-
After a successful installation by using the step CLI, open a command prompt in your user profile folder (Win+R type %USERPROFILE%).
47
+
After a successful installation by using the step CLI, open a Command Prompt window in your user profile folder (`Win+R type %USERPROFILE%`).
47
48
48
49
1. To create root and intermediate certificates, run the following command. Remember the password, which you need to use in the next step.
1. Use the certificate authority (CA) files generated to create a certificate for the client. Make sure to use the correct path for the cert and secrets files in the command.
55
+
1. To create a certificate for the client, use the certificate authority (CA) files generated. Make sure to use the correct path for the cert and secrets files in the command.
To keep the quickstart simple, you create a namespace with minimal properties. For detailed steps about configuring network, security, and other settings on other pages of the wizard, see [Create and manage namespaces](create-view-manage-namespaces.md).
75
+
To keep this quickstart simple, create a namespace with minimal properties. For more information about network, security, and settings on other tabs, see [Create and manage namespaces](create-view-manage-namespaces.md).
75
76
76
77
## Create clients
77
78
78
-
Use the command to create the client. Update the command with your resource group and a namespace name.
79
+
Use this command to create the client. Update the command with your resource group and a namespace name.
- To keep the quickstart simple, you use thumbprint match for authentication. For steps on how to use the X.509 CA certificate chain for client authentication, see [Client authentication using certificate chain](./mqtt-certificate-chain-client-authentication.md).
84
-
- For this exercise, we use the default `$all client` group, which includes all the clients in the namespace. To learn more about creating custom client groups by using client attributes, see [Client groups](mqtt-client-groups.md).
84
+
85
+
- To keep this quickstart simple, use thumbprint match for authentication. For steps on how to use the X.509 CA certificate chain for client authentication, see [Client authentication using certificate chain](./mqtt-certificate-chain-client-authentication.md).
86
+
- For this exercise, use the default `$all client` group, which includes all the clients in the namespace. To learn more about creating custom client groups by using client attributes, see [Client groups](mqtt-client-groups.md).
85
87
86
88
## Create topic spaces
87
89
88
-
Use the command to create the topic space. Update the command with your resource group, namespace name, and topic space name.
90
+
Use this command to create the topic space. Update the command with your resource group, namespace name, and topic space name.
Use the `az eventgrid` command to create the first permission binding for publisher permission. Update the command with your resource group, namespace name, and permission binding name.
Use the command to create the second permission binding. Update the command with your resource group, namespace name, and permission binding name. This permission binding is for subscribers.
104
+
Use this command to create the second permission binding. Update the command with your resource group, namespace name, and permission binding name. This permission binding is for subscribers.
103
105
104
106
```azurecli-interactive
105
-
az eventgrid namespace permission-binding create -g {Resource Group} --namespace-name {Namespace Name} -n {Name of second Permission Binding} --client-group-name '$all' --permission subscriber --topic-space-name {Topicspace Name}
107
+
az eventgrid namespace permission-binding create --resource-group {Resource Group} --namespace-name {Namespace Name} --name {Name of second Permission Binding} --client-group-name '$all' --permission subscriber --topic-space-name {Topicspace Name}
106
108
```
107
109
108
110
## Publish and subscribe to MQTT messages
109
111
110
-
The following sample code is a simple .NET publisher that attempts to connect and publish to a namespace and subscribes to the MQTT topic. You can use the code to modify according to your requirement and run the code in Visual Studio or any of your favorite tools.
112
+
The following sample code is a simple .NET publisher that attempts to connect and publish to a namespace and subscribes to the MQTT topic. You can modify the code for your requirements and run it in Visual Studio or another design tool.
111
113
112
-
You need to install the MQTTnet package (version 4.1.4.563) from NuGet to run this code. (In Visual Studio, right-click the project name in Solution Explorer, go to **Manage NuGet packages**, and search for **MQTTnet**. Select **MQTTnet package** and install.)
114
+
You need to install the MQTTnet package (version 4.1.4.563) from NuGet to run this code. In Visual Studio, right-click the project name in Solution Explorer, go to **Manage NuGet packages**, and search for **MQTTnet**. Select **MQTTnet package** and install.
113
115
114
116
> [!NOTE]
115
-
>The following sample code is for demonstration purposes only and isn't intended for production use.
117
+
>The following sample code is for demonstration purposes only and isn't intended for production use.
116
118
117
119
### Sample C# code to connect a client, publish, and subscribe to an MQTT message on a topic
118
120
@@ -163,8 +165,9 @@ while (true)
163
165
164
166
You can replicate and modify the same code for multiple clients to publish and subscribe among the clients.
165
167
166
-
## Next steps
168
+
## Related content
167
169
168
170
-[Tutorial: Route MQTT messages to Azure Event Hubs using namespace topics](mqtt-routing-to-event-hubs-portal-namespace-topics.md)
169
171
-[Tutorial: Route MQTT messages to Azure Functions using custom topics](mqtt-routing-to-azure-functions-portal.md)
170
-
- For code samples, go to [this GitHub repository](https://github.com/Azure-Samples/MqttApplicationSamples/tree/main).
172
+
173
+
For code samples, go to [this GitHub repository](https://github.com/Azure-Samples/MqttApplicationSamples/tree/main).
0 commit comments