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
title: 'Quickstart: Publish and subscribe on an MQTT topic using CLI'
3
-
description: 'Quickstart guide to use Azure Event Grid’s MQTT broker feature and Azure CLI to publish and subscribe MQTT messages on a topic'
2
+
title: 'Quickstart: Publish and subscribe on an MQTT topic by using the CLI'
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
@@ -12,113 +12,112 @@ ms.author: geguirgu
12
12
ms.subservice: mqtt
13
13
---
14
14
15
-
# Quickstart: Publish and subscribe to MQTT messages on Event Grid Namespace with Azure CLI
15
+
# Quickstart: Publish and subscribe to MQTT messages on an Event Grid namespace with the Azure CLI
16
16
17
-
Azure Event Grid’s MQTT broker feature supports messaging using the MQTT protocol. Clients (both devices and cloud applications) can publish and subscribe MQTT messages over flexible hierarchical topics for scenarios such as highscale broadcast, and command & control.
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
18
19
+
In this article, you use the Azure CLI to:
19
20
20
-
In this article, you use the Azure CLI to do the following tasks:
21
-
1. Create an Event Grid namespace and enable MQTT broker
22
-
2. Create subresources such as clients, client groups, and topic spaces
23
-
3. Grant clients access to publish and subscribe to topic spaces
24
-
4. Publish and receive MQTT messages
21
+
- Create an Event Grid namespace and enable the MQTT broker.
22
+
- Create subresources such as clients, client groups, and topic spaces.
23
+
- Grant clients access to publish and subscribe to topic spaces.
24
+
- Publish and receive MQTT messages.
25
+
26
+
If you don't have an [Azure subscription](/azure/guides/developer/azure-developer-guide#understanding-accounts-subscriptions-and-billing), create an [Azure free account](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio) before you begin.
25
27
26
28
## Prerequisites
27
-
- If you don't have an [Azure subscription](/azure/guides/developer/azure-developer-guide#understanding-accounts-subscriptions-and-billing), create an [Azure free account](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio) before you begin.
28
-
- If you're new to Azure Event Grid, read through [Event Grid overview](/azure/event-grid/overview) before starting this tutorial.
29
-
- Register the Event Grid resource provider as per[Register the Event Grid resource provider](/azure/event-grid/custom-event-quickstart-portal#register-the-event-grid-resource-provider).
30
-
- 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 environments.
29
+
30
+
- If you're new to Event Grid, read through the [Event Grid overview](/azure/event-grid/overview) before you start this tutorial.
31
+
- Register the Event Grid resource provider according to the steps in[Register the Event Grid resource provider](/azure/event-grid/custom-event-quickstart-portal#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.
31
33
- Use the Bash environment in [Azure Cloud Shell](/azure/cloud-shell/overview). For more information, see [Quickstart for Bash in Azure Cloud Shell](/azure/cloud-shell/quickstart).
32
-
- 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 Azure CLI in a Docker container. For more information, see [How to run the Azure CLI in a Docker container](/cli/azure/run-azure-cli-docker).
33
-
- 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 displayed in your terminal. For other sign-in options, see [Sign in with the Azure CLI](/cli/azure/authenticate-azure-cli).
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).
34
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).
35
37
- 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).
36
-
- This article requires version 2.53.1 or later of the Azure CLI. If using Azure Cloud Shell, the latest version is already installed.
38
+
- 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.
37
39
- You need an X.509 client certificate to generate the thumbprint and authenticate the client connection.
38
-
- Review the Event Grid namespace [CLI documentation](/cli/azure/eventgrid/namespace)
40
+
- Review the Event Grid namespace [CLI documentation](/cli/azure/eventgrid/namespace).
39
41
40
-
## Generate sample client certificate and thumbprint
41
-
If you don't already have a certificate, you can create a sample certificate using the [step CLI](https://smallstep.com/docs/step-cli/installation/). Consider installing manually for Windows.
42
+
## Generate a sample client certificate and thumbprint
42
43
43
-
After a successful installation of Step, you should open a command prompt in your user profile folder (Win+R type %USERPROFILE%).
44
+
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.
44
45
45
-
1. To create root and intermediate certificates, run the following command. Remember the password, which needs to be used in the next step.
46
+
After a successful installation by using the step CLI, open a command prompt in your user profile folder (Win+R type %USERPROFILE%).
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.
> To keep the QuickStart simple, you'll 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).
74
+
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).
73
75
74
76
## Create clients
75
77
76
-
Use the command to create the client. Update the command with your Resource group, and a Namespace name.
78
+
Use the command to create the client. Update the command with your resource group and a namespace name.
> - To keep the QuickStart simple, you'll be using Thumbprint match for authentication. For detailed steps on using X.509 CA certificate chain for client authentication, see [client authentication using certificate chain](./mqtt-certificate-chain-client-authentication.md).
84
-
> - Also 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 using client attributes, see [client groups](mqtt-client-groups.md) document.
83
+
- 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).
85
85
86
86
## Create topic spaces
87
87
88
-
Use the command to create the topic space. Update the command with your Resource group, namespace name, and a topic space name.
88
+
Use the command to create the topic space. Update the command with your resource group, namespace name, and topic space name.
Use the az resource command to create the first permission binding for publisher permission. Update the command with your Resource group, namespace name, and a permission binding name.
96
+
Use the `az resource` 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 a permission binding name. This permission binding is for subscriber.
102
+
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.
103
103
104
104
```azurecli-interactive
105
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 publisher --topic-space-name {Topicspace Name}
106
106
```
107
107
108
-
## Publish and subscribe MQTT messages
108
+
## Publish and subscribe to MQTT messages
109
109
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 per your requirement and run the code in Visual Studio or any of your favorite tools.
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.
111
111
112
-
You need to install the MQTTnet package (version 4.1.4.563) from NuGet to run this code.
113
-
(In Visual Studio, right click on the project name in Solution Explorer, go to Manage NuGet packages, search for MQTTnet. Select MQTTnet package and install.)
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
113
115
114
> [!NOTE]
116
-
>The following sample code is only for demonstration purposes and is not intended for production use.
115
+
>The following sample code is for demonstration purposes only and isn't intended for production use.
117
116
118
-
**Sample C# code to connect a client, publish/subscribe MQTT message on a topic**
117
+
### Sample C# code to connect a client, publish, and subscribe to an MQTT message on a topic
119
118
120
119
> [!IMPORTANT]
121
-
> Please update the client certificate and key pem file paths depending on location of your client certificate files. Also, ensure the client authentication name, topic information match with your configuration.
120
+
> Update the client certificate and key pem file paths depending on the location of your client certificate files. Also, ensure that the client authentication name and topic information match with your configuration.
122
121
123
122
```csharp
124
123
usingMQTTnet.Client;
@@ -162,8 +161,9 @@ while (true)
162
161
163
162
```
164
163
165
-
You can replicate and modify the same code for multiple clients to perform publish / subscribe among the clients.
164
+
You can replicate and modify the same code for multiple clients to publish and subscribe among the clients.
166
165
167
166
## Next steps
168
-
-[Route MQTT messages to Event Hubs](mqtt-routing-to-event-hubs-cli.md)
169
-
- For code samples, go to [this repository.](https://github.com/Azure-Samples/MqttApplicationSamples/tree/main)
167
+
168
+
-[Route MQTT messages to Event Hubs](mqtt-routing-to-event-hubs-cli.md).
169
+
- For code samples, go to [this GitHub repository](https://github.com/Azure-Samples/MqttApplicationSamples/tree/main).
0 commit comments