Skip to content

Commit 62e04ec

Browse files
Freshness.
1 parent 0f49e6c commit 62e04ec

File tree

1 file changed

+86
-76
lines changed

1 file changed

+86
-76
lines changed
Lines changed: 86 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
---
2-
title: Publish and consume events using namespace topics
3-
description: This article provides step-by-step instructions to publish events to Azure Event Grid in the CloudEvents JSON format and consume those events by using the pull delivery model.
2+
title: "Quickstart: Publish and Consume Events Using Namespace Topics"
3+
description: Learn how to publish events to Azure Event Grid in the CloudEvents JSON format and consume those events by using the pull delivery model.
44
ms.topic: quickstart
55
ms.custom: ignite-2023, devx-track-azurecli, build-2024
66
ms.author: robece
77
author: robece
8-
ms.date: 02/20/2024
8+
ms.date: 06/23/2025
9+
#customer intent: As a developer, I want to use the pull delivery model to publish and consume events using Azure Event Grid.
910
---
1011

11-
# Publish to namespace topics and consume events in Azure Event Grid
12+
# Quickstart: Publish to namespace topics and consume events in Azure Event Grid
1213

13-
This article provides a quick introduction to pull delivery using the `curl` bash shell command to publish, receive, and acknowledge events. Use Azure CLI commands to create Azure Event Grid resources. This article is suitable for a quick test of the pull delivery functionality.
14+
In this quickstart, you do pull delivery using the `curl` bash shell command to publish, receive, and acknowledge events. Use Azure CLI commands to create Azure Event Grid resources. This article is suitable for a quick test of the pull delivery functionality.
1415

1516
For sample code that uses the data plane SDKs, see these resources:
1617

@@ -28,20 +29,21 @@ For more information about the pull delivery model, see [Azure Event Grid namesp
2829

2930
## Create a resource group
3031

31-
Create an Azure resource group with the [az group create](/cli/azure/group#az-group-create) command. You use this resource group to contain all resources created in this article.
32+
Create an Azure resource group with the [az group create](/cli/azure/group#az-group-create) command. Use this resource group to contain all resources you create in this article.
3233

3334
The general steps to use Cloud Shell to run commands are:
3435

3536
- Select **Open Cloud Shell** to see an Azure Cloud Shell window on the right pane.
3637
- Copy the command and paste into the Azure Cloud Shell window.
3738
- Press ENTER to run the command.
3839

39-
1. Declare a variable to hold the name of an Azure resource group. Specify a name for the resource group by replacing `<your-resource-group-name>` with a value you like.
40+
1. Declare a variable to hold the name of an Azure resource group. Specify a name for the resource group by replacing `<your-resource-group-name>` with your value.
4041

4142
```azurecli-interactive
4243
resource_group="<your-resource-group-name>"
4344
```
44-
1. Create a resource group. Change the location as you see fit.
45+
46+
1. Create a resource group. You can change the location to any Azure location.
4547
4648
```azurecli-interactive
4749
az group create --name $resource_group --location eastus
@@ -55,135 +57,143 @@ An Event Grid namespace provides a user-defined endpoint to which you post your
5557
5658
- It should be between 3-50 characters.
5759
- It should be regionally unique.
58-
- Only allowed characters are a-z, A-Z, 0-9 and -
60+
- Only allowed characters are: a-z, A-Z, 0-9, and -
5961
- It shouldn't start with reserved key word prefixes like `Microsoft`, `System`, or `EventGrid`.
6062
61-
1. Declare a variable to hold the name for your Event Grid namespace. Specify a name for the namespace by replacing `<your-namespace-name>` with a value you like.
63+
1. Declare a variable to hold the name for your Event Grid namespace. Specify a name for the namespace by replacing `<your-namespace-name>` with your value.
64+
65+
```azurecli-interactive
66+
namespace="<your-namespace-name>"
67+
```
6268

63-
```azurecli-interactive
64-
namespace="<your-namespace-name>"
65-
```
6669
1. Create a namespace. You might want to change the location where you deploy it.
6770

68-
```azurecli-interactive
69-
az eventgrid namespace create --resource-group $resource_group --name $namespace --location eastus
70-
```
71+
```azurecli-interactive
72+
az eventgrid namespace create --resource-group $resource_group --name $namespace --location eastus
73+
```
7174

7275
## Create a namespace topic
7376

74-
Create a topic that's used to hold all events published to the namespace endpoint.
77+
Create a topic that holds all events published to the namespace endpoint.
7578

76-
1. Declare a variable to hold the name for your namespace topic. Specify a name for the namespace topic by replacing `<your-topic-name>` with a value you like.
79+
1. Declare a variable to hold the name for your namespace topic. Specify a name for the namespace topic by replacing `<your-topic-name>` with your value.
80+
81+
```azurecli-interactive
82+
topic="<your-topic-name>"
83+
```
7784

78-
```azurecli-interactive
79-
topic="<your-topic-name>"
80-
```
8185
1. Create your namespace topic:
8286

83-
```azurecli-interactive
84-
az eventgrid namespace topic create --resource-group $resource_group --name $topic --namespace-name $namespace
85-
```
87+
```azurecli-interactive
88+
az eventgrid namespace topic create --resource-group $resource_group --name $topic --namespace-name $namespace
89+
```
8690

8791
## Create an event subscription
8892

8993
Create an event subscription setting its delivery mode to *queue*, which supports [pull delivery](pull-delivery-overview.md). For more information on all configuration options, see [Azure Event Grid REST API](/rest/api/eventgrid).
9094

91-
1. Declare a variable to hold the name for an event subscription to your namespace topic. Specify a name for the event subscription by replacing `<your-event-subscription-name>` with a value you like.
95+
1. Declare a variable to hold the name for an event subscription to your namespace topic. Specify a name for the event subscription by replacing `<your-event-subscription-name>` with your value.
96+
97+
```azurecli-interactive
98+
event_subscription="<your-event-subscription-name>"
99+
```
92100

93-
```azurecli-interactive
94-
event_subscription="<your-event-subscription-name>"
95-
```
96101
1. Create an event subscription to the namespace topic:
97102

98-
```azurecli-interactive
99-
az eventgrid namespace topic event-subscription create --resource-group $resource_group --topic-name $topic --name $event_subscription --namespace-name $namespace --delivery-configuration "{deliveryMode:Queue,queue:{receiveLockDurationInSeconds:300,maxDeliveryCount:4,eventTimeToLive:P1D}}"
100-
```
103+
```azurecli-interactive
104+
az eventgrid namespace topic event-subscription create --resource-group $resource_group --topic-name $topic --name $event_subscription --namespace-name $namespace --delivery-configuration "{deliveryMode:Queue,queue:{receiveLockDurationInSeconds:300,maxDeliveryCount:4,eventTimeToLive:P1D}}"
105+
```
101106

102107
## Send events to your topic
103-
Now, send a sample event to the namespace topic by following steps in this section.
108+
109+
Send a sample event to the namespace topic by following steps in this section.
104110

105111
### List namespace access keys
106112

107-
1. Get the access keys associated with the namespace you created. You use one of them to authenticate when publishing events. To list your keys, you need the full namespace resource ID first. Get it by running the following command:
113+
1. Get the access keys associated with the namespace you created. You need one of them to authenticate when publishing events. To list your keys, get the full namespace resource ID. Run the following command:
108114

109-
```azurecli-interactive
110-
namespace_resource_id=$(az eventgrid namespace show --resource-group $resource_group --name $namespace --query "id" --output tsv)
115+
```azurecli-interactive
116+
namespace_resource_id=$(az eventgrid namespace show --resource-group $resource_group --name $namespace --query "id" --output tsv)
117+
```
111118

112-
```
113119
1. Get the first key from the namespace:
114120

115-
```azurecli-interactive
116-
key=$(az eventgrid namespace list-key -g $resource_group --namespace-name $namespace --query "key1" --output tsv)
117-
```
121+
```azurecli-interactive
122+
key=$(az eventgrid namespace list-key --resource-group $resource_group --namespace-name $namespace --query "key1" --output tsv)
123+
```
118124

119125
### Publish an event
120126

121127
1. Retrieve the namespace hostname. You use it to compose the namespace HTTP endpoint to which events are sent. The following operations were first available with API version `2023-06-01-preview`.
122128

123-
```azurecli-interactive
124-
publish_operation_uri="https://"$(az eventgrid namespace show -g $resource_group -n $namespace --query "topicsConfiguration.hostname" --output tsv)"/topics/"$topic:publish?api-version=2023-06-01-preview
125-
```
129+
```azurecli-interactive
130+
publish_operation_uri="https://"$(az eventgrid namespace show -g $resource_group -n $namespace --query "topicsConfiguration.hostname" --output tsv)"/topics/"$topic:publish?api-version=2023-06-01-preview
131+
```
132+
126133
1. Create a sample [CloudEvents](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/formats/json-format.md) compliant event:
127134

128-
```azurecli-interactive
129-
event=' { "specversion": "1.0", "id": "'"$RANDOM"'", "type": "com.yourcompany.order.ordercreatedV2", "source" : "/mycontext", "subject": "orders/O-234595", "time": "'`date +%Y-%m-%dT%H:%M:%SZ`'", "datacontenttype" : "application/json", "data":{ "orderId": "O-234595", "url": "https://yourcompany.com/orders/o-234595"}} '
130-
```
135+
```azurecli-interactive
136+
event=' { "specversion": "1.0", "id": "'"$RANDOM"'", "type": "com.yourcompany.order.ordercreatedV2", "source" : "/mycontext", "subject": "orders/O-234595", "time": "'`date +%Y-%m-%dT%H:%M:%SZ`'", "datacontenttype" : "application/json", "data":{ "orderId": "O-234595", "url": "https://yourcompany.com/orders/o-234595"}} '
137+
```
131138

132-
The `data` element is the payload of your event. Any well-formed JSON can go in this field. Properties that can go into an event are also known as *context attributes*. For more information, see [CloudEvents](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/spec.md).
139+
The `data` element is the payload of your event. Any well-formed JSON can go in this field. Properties that can go into an event are also known as *context attributes*. For more information, see [CloudEvents](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/spec.md).
133140

134141
1. Use CURL to send the event to the topic. CURL is a utility that sends HTTP requests.
135142

136-
```azurecli-interactive
137-
curl -X POST -H "Content-Type: application/cloudevents+json" -H "Authorization:SharedAccessKey $key" -d "$event" $publish_operation_uri
138-
```
143+
```azurecli-interactive
144+
curl -X POST -H "Content-Type: application/cloudevents+json" -H "Authorization:SharedAccessKey $key" -d "$event" $publish_operation_uri
145+
```
139146

140147
### Receive the event
141148

142149
You receive events from Event Grid using an endpoint that refers to an event subscription.
143150

144-
1. Compose that endpoint by running the following command:
151+
1. Compose the endpoint by running the following command:
145152

146-
```azurecli-interactive
147-
receive_operation_uri="https://"$(az eventgrid namespace show --resource-group $resource_group --name $namespace --query "topicsConfiguration.hostname" --output tsv)"/topics/"$topic/eventsubscriptions/$event_subscription:receive?api-version=2023-06-01-preview
148-
```
153+
```azurecli-interactive
154+
receive_operation_uri="https://"$(az eventgrid namespace show --resource-group $resource_group --name $namespace --query "topicsConfiguration.hostname" --output tsv)"/topics/"$topic/eventsubscriptions/$event_subscription:receive?api-version=2023-06-01-preview
155+
```
149156

150157
1. Submit a request to consume the event:
151158

152-
```azurecli-interactive
153-
curl -X POST -H "Content-Type: application/json" -H "Authorization:SharedAccessKey $key" $receive_operation_uri
154-
```
159+
```azurecli-interactive
160+
curl -X POST -H "Content-Type: application/json" -H "Authorization:SharedAccessKey $key" $receive_operation_uri
161+
```
155162

156163
### Acknowledge an event
157164

158165
After you receive an event, you pass that event to your application for processing. After you successfully process your event, you no longer need that event to be in your event subscription. To instruct Event Grid to delete the event, you **acknowledge** it using its lock token that you got on the receive operation's response.
159166

160167
1. In the previous section, you received a response that includes a `brokerProperties` object with a `lockToken` property. Copy the lock token value and set it on an environment variable:
161168

162-
```azurecli-interactive
163-
lockToken="<paste-the-lock-token-here>"
164-
```
165-
1. Now, build the acknowledge operation payload, which specifies the lock token for the event you want to be acknowledged.
169+
```azurecli-interactive
170+
lockToken="<paste-the-lock-token-here>"
171+
```
172+
173+
1. Build the acknowledge operation payload, which specifies the lock token for the event you want to be acknowledged.
174+
175+
```azurecli-interactive
176+
acknowledge_request_payload=' { "lockTokens": ["'$lockToken'"]} '
177+
```
166178

167-
```azurecli-interactive
168-
acknowledge_request_payload=' { "lockTokens": ["'$lockToken'"]} '
169-
```
170179
1. Proceed with building the string with the acknowledge operation URI:
171180

172-
```azurecli-interactive
173-
acknowledge_operation_uri="https://"$(az eventgrid namespace show -g $resource_group -n $namespace --query "topicsConfiguration.hostname" --output tsv)"/topics/"$topic/eventsubscriptions/$event_subscription:acknowledge?api-version=2023-06-01-preview
174-
```
181+
```azurecli-interactive
182+
acknowledge_operation_uri="https://"$(az eventgrid namespace show -g $resource_group -n $namespace --query "topicsConfiguration.hostname" --output tsv)"/topics/"$topic/eventsubscriptions/$event_subscription:acknowledge?api-version=2023-06-01-preview
183+
```
184+
175185
1. Finally, submit a request to acknowledge the event received:
176186

177-
```azurecli-interactive
178-
curl -X POST -H "Content-Type: application/json" -H "Authorization:SharedAccessKey $key" -d "$acknowledge_request_payload" $acknowledge_operation_uri
179-
```
180-
181-
If the acknowledge operation is executed before the lock token expires (300 seconds as set when we created the event subscription), you should see a response like the following example:
182-
183-
```json
184-
{"succeededLockTokens":["CiYKJDQ4NjY5MDEyLTk1OTAtNDdENS1BODdCLUYyMDczNTYxNjcyMxISChDZae43pMpE8J8ovYMSQBZS"],"failedLockTokens":[]}
185-
```
186-
187+
```azurecli-interactive
188+
curl -X POST -H "Content-Type: application/json" -H "Authorization:SharedAccessKey $key" -d "$acknowledge_request_payload" $acknowledge_operation_uri
189+
```
190+
191+
If the acknowledge operation is executed before the lock token expires (300 seconds as set when we created the event subscription), you should see a response like the following example:
192+
193+
```json
194+
{"succeededLockTokens":["CiYKJDQ4NjY5MDEyLTk1OTAtNDdENS1BODdCLUYyMDczNTYxNjcyMxISChDZae43pMpE8J8ovYMSQBZS"],"failedLockTokens":[]}
195+
```
196+
187197
## Next step
188198

189199
To learn more about pull delivery model, see [Pull delivery with HTTP](pull-delivery-overview.md).

0 commit comments

Comments
 (0)