You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
While system topics let you react to Azure service events, custom topics allow you to publish your own events from any application. Event domains take this further by providing a single management endpoint for thousands of related topics. This lesson covers how to create and use custom topics, how to secure them, and when to use event domains for large-scale scenarios.
A custom topic is an Event Grid endpoint that you create to publish events from your own applications. Any service — a web API, a background worker, an IoT device — can send events to a custom topic using HTTP POST.
az eventgrid topic create \
--name app-events \
--resource-group rg-messaging \
--location uksouth \
--input-schema eventgridschema
After creation, the topic has an endpoint URL and two access keys:
az eventgrid topic show --name app-events --resource-group rg-messaging --query "endpoint"
az eventgrid topic key list --name app-events --resource-group rg-messaging
import { EventGridPublisherClient, AzureKeyCredential } from '@azure/eventgrid';
const client = new EventGridPublisherClient(
'https://app-events.uksouth-1.eventgrid.azure.net/api/events',
'EventGrid',
new AzureKeyCredential(topicKey)
);
await client.send([
{
eventType: 'App.User.Registered',
subject: '/users/user-123',
dataVersion: '1.0',
data: {
userId: 'user-123',
email: 'alice@example.com',
plan: 'premium',
},
},
]);
You can publish up to 5,000 events per request (or 1 MB total). Events are delivered to subscribers within seconds.
# Subscribe an Azure Function to the custom topic
az eventgrid event-subscription create \
--name handle-registrations \
--source-resource-id /subscriptions/{sub-id}/resourceGroups/rg-messaging/providers/Microsoft.EventGrid/topics/app-events \
--endpoint /subscriptions/{sub-id}/resourceGroups/rg-messaging/providers/Microsoft.Web/sites/myApp/functions/OnUserRegistered \
--endpoint-type azurefunction \
--included-event-types App.User.Registered
Every custom topic has two access keys. Include the key in the aeg-sas-key header when publishing. Rotate keys regularly and store them in Azure Key Vault.
For temporary, scoped access, generate a Shared Access Signature (SAS) token:
az eventgrid topic generate-sas --name app-events --resource-group rg-messaging \
--expiration-date-utc "2025-12-31T23:59:59Z"
For Azure-to-Azure scenarios, use managed identities to authenticate without storing keys:
import { DefaultAzureCredential } from '@azure/identity';
const client = new EventGridPublisherClient(
topicEndpoint,
'EventGrid',
new DefaultAzureCredential()
);
When subscribing an HTTP webhook to Event Grid, the endpoint must prove ownership by responding to a validation handshake:
SubscriptionValidation event with a validationCodeThis prevents anyone from subscribing arbitrary endpoints to your topics.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.