Ever wonder how apps send real-time notifications, like delivery updates or alerts?
Let’s build a system for that using Azure Event Grid and WebHooks!
Here's how it will work:
✅ An e-commerce system generates an event "Order Shipped"
✅ This event is published to Azure Event Grid.
✅ Event Grid routes the event to web-hook subscribers in real time..
✅ Notification could be Email, SMS, or in-app Notifications.
In this tutorial, we’ll use Logic App to represent the business system that generates an order shipped event.
The webhook is an Azure Function which is going to receive the notification from Event Grid and dispatch the notifications appropriately to the right quarters (via email, sms or in-app)
What is Azure Event Grid?
Event Grid is an Azure service for building event-driven applications. It has a push delivery mechanism to send events to destinations like webhooks or Azure services. You can configure it to deliver events to a wide range of endpoints and automate workflows by receiving events from over 20 Azure services.
The pull delivery mode allows clients to connect to Event Grid to read events.
Event Grid supports two types of messaging: MTTQ (Message Throughput Queues) and Event Messaging (HTTP), enabling flexibility in how events are processed.
In this use case, we are using a push method. Event Grid pushes events to the subscribed webhook endpoints whenever an event occurs. This eliminates the need for the consumer to constantly poll for updates, making the system more efficient and responsive. Let’s get started:
Step 1: Set Up Event Grid Topic
Events published to Event Grid land on a topic, which is a resource that logically contains all events. We will create a system topic which typically represents one or more events published by Azure services.
In Azure Portal, search for "Event Grid Topics" and create a new topic.
Note the Topic Endpoint and Key for authentication. (found in Settings/Access Keys)
Step 2: Set Up Event Source
Go to the Azure Portal, create a Logic App.
Add a Recurrence Trigger to simulate order updates (e.g., every minute).
Use an HTTP Action to send an event payload (JSON) to Event Grid.
- Add a header for authentication:
“aeg-sas-key: <your_topic_key>”
Step 3: Deploy a sample Web-hook endpoint
The webhook can be any http based service.
For instance, in the case of in-app notifications: The webhook will likely be a service which communicates with platforms like Firebase Cloud Messaging (FCM) or Apple Push Notification Service (APNs) to send real-time notifications to users’ devices.
In this demo, we will simulate this using an Azure Function that receives the event notification. If you like, you can take advantage of the native Event Grid trigger for Azure functions but here we make use of http trigger so as to simulate a typical external http service.
Create an Azure Function (http trigger)
Write appropriate code to handle the notification received.
Notice that the Azure Function contains extra code before handling the actual request. This is because of Event Grid web-hook validation.
Here is how the validation works:
The web-hook must listen for the Validation Event:
Event Grid will send a POST request with the SubscriptionValidationEvent to the webhook's endpoint.
The payload will include a validationCode inside the data property.
The webhook must reply with an HTTP 200 OK response.
The response body must include a JSON object with the validationResponse key containing the received validationCode.
Here is a Microsoft doc explaining this in details: Troubleshoot Azure Event Grid subscription validations
Step 4: Subscribe the WebHook to the Event Grid Topic
In the Azure Portal, navigate to your Event Grid Topic.
Add a new Event Subscription
Set the Endpoint Type to "WebHook".
Provide the WebHook URL (in this case, it is the Azure Function URL).
Subscription created:
Test:
Trigger events using the Logic App.
Verify the web-hook endpoint logs to confirm the event was received.
Finally. Check that it Event Grid publishes this message to the web-hook URL (Azure function). We can easily confirm this from the Function App’s log stream:
In this post, we've demonstrated how to build an efficient, event-driven notification system using Azure Event Grid and Webhooks. We've also covered important considerations like webhook validation. By using Event Grid's push delivery, we can integrate Azure Functions and third-party services to automate real-time notifications, improving application responsiveness and ensuring instant delivery of critical updates.
That’s all for today, see you in the next one.