July 18th, 2024

Announcing the stable release of Azure Event Grid Namespaces HTTP client libraries

Ronnie Geraghty
Product Manager

We’re thrilled to announce the first stable release of the HTTP libraries for the Namespaces feature in Azure Event Grid, now available for .NET, Java, JavaScript, Python, and Go. This release marks a significant milestone in our journey to provide robust, reliable, and easy-to-use tools for developers building event-driven applications on Azure.

Azure Event Grid is a highly scalable, fully managed service that facilitates Pub/Sub message distribution using MQTT and HTTP protocols. Azure Event Grid Namespaces, a feature of the Standard tier of Event Grid, enhances this experience by allowing you to logically group related events and manage them under a single umbrella. This functionality is useful for complex applications handling a large number of events from diverse sources. With namespaces, you can apply consistent policies, security settings, and routing rules, making it easier to manage and scale your event-driven solutions.

The stable release introduces client libraries that bring the power of Event Grid Namespaces to your favorite programming languages. These libraries are designed to be user-friendly, high-performing, and reliable. These libraries come with comprehensive documentation and code samples to help you get started quickly. These client libraries offer a streamlined API that simplifies the process of sending and receiving messages within your applications.

These client libraries greatly enhance your ability to build and manage event-driven applications on Azure. In the following sections, we provide detailed instructions on how to get started with these libraries, including installation, initialization, and usage examples. Let’s dive in!

Language-specific packages

Language Package and version
.NET Azure.Messaging.EventGrid.Namespaces v1.0.0
Go aznamespaces v1.0.0
Java azure-messaging-eventgrid-namespaces v1.0.0
JavaScript @azure/eventgrid-namespaces v1.0.0
Python azure-eventgrid v4.20.0

TIP: The Python SDK didn’t create a new package for Event Grid Namespaces. Instead, this functionality is included in the existing Event Grid library.

  • The EventGridPublisherClient creates an Event Grid Namespace Publisher client when specifying the namespace_topic parameter.
    • client = EventGridClient(endpoint, credential, namespace_topic=<your-topic-name>)
  • The EventGridConsumerClient is a new client only available for Event Grid Namespace.

Get started with .NET

Create an Event Grid Namespace resource on Azure

  • You need an Azure subscription to use Azure Event Grid Namespaces. If you don’t have an existing Azure account, you may sign up for a free trial or use your Visual Studio subscriber benefits when you create an account.
  • You can follow these instructions to create an Event Grid Namespace resource on Azure.

Installation

To install the library, run the following command using the .NET CLI:

  dotnet add package Azure.Messaging.EventGrid.Namespaces --version 1.0.0

Send events

using Azure;
using Azure.Messaging;
using Azure.Messaging.EventGrid.Namespaces;

/* Construct the client using an Endpoint for a namespace 
as well as the shared access key*/
var senderClient = new EventGridSenderClient(
  new Uri(namespaceTopicHost), 
  topicName, 
  new AzureKeyCredential(namespaceKey));

await senderClient.SendAsync(
    new[] {
        new CloudEvent(
          "employee_source", 
          "type", 
          new DataModel { Name = "Tom", Age = 55 }),
        new CloudEvent(
          "employee_source", 
          "type", 
          new DataModel { Name = "Alice", Age = 25 })
    });

Receive & handle events

using Azure;
using Azure.Messaging;
using Azure.Messaging.EventGrid.Namespaces;

// Construct the client using an Endpoint for a namespace as well as the shared access key
var receiverClient = new EventGridReceiverClient(
  new Uri(namespaceTopicHost), 
  topicName, 
  subscriptionName, 
  new AzureKeyCredential(namespaceKey));

ReceiveResult result = await receiverClient.ReceiveAsync(maxEvents: 3);

// Iterate through the results and collect the lock tokens for events we want to release/acknowledge/result
var toRelease = new List<string>();
var toAcknowledge = new List<string>();
var toReject = new List<string>();
foreach (ReceiveDetails detail in result.Details)
{
    CloudEvent @event = detail.Event;
    BrokerProperties brokerProperties = detail.BrokerProperties;
    Console.WriteLine(@event.Data.ToString());

    // The lock token is used to acknowledge, reject or release the event
    Console.WriteLine(brokerProperties.LockToken);

    var data = @event.Data.ToObjectFromJson<DataModel>();
    // If the data from the event has Name "Bob", we are not able to acknowledge it yet,
    // so we release it, thereby allowing other consumers to receive it.
    if (data.Name == "Bob")
    {
        toRelease.Add(brokerProperties.LockToken);
    }
    // If the data is for "Tom", we will acknowledge it thereby deleting it from the subscription.
    else if (data.Name == "Tom")
    {
        toAcknowledge.Add(brokerProperties.LockToken);
    }
    // reject all other events which will move the event to the dead letter queue if it is configured
    else
    {
        toReject.Add(brokerProperties.LockToken);
    }
}

if (toRelease.Count > 0)
{
    ReleaseResult releaseResult = await receiverClient.ReleaseAsync(toRelease);

    // Inspect the Release result
    Console.WriteLine($"Failed count for Release: {releaseResult.FailedLockTokens.Count}");
    foreach (FailedLockToken failedLockToken in releaseResult.FailedLockTokens)
    {
        Console.WriteLine($"Lock Token: {failedLockToken.LockToken}");
        Console.WriteLine($"Error Code: {failedLockToken.Error.Code}");
        Console.WriteLine($"Error Description: {failedLockToken.Error.Message}");
    }

    Console.WriteLine($"Success count for Release: {releaseResult.SucceededLockTokens.Count}");
    foreach (string lockToken in releaseResult.SucceededLockTokens)
    {
        Console.WriteLine($"Lock Token: {lockToken}");
    }
}

if (toAcknowledge.Count > 0)
{
    AcknowledgeResult acknowledgeResult = await receiverClient.AcknowledgeAsync(toAcknowledge);

    // Inspect the Acknowledge result
    Console.WriteLine($"Failed count for Acknowledge: {acknowledgeResult.FailedLockTokens.Count}");
    foreach (FailedLockToken failedLockToken in acknowledgeResult.FailedLockTokens)
    {
        Console.WriteLine($"Lock Token: {failedLockToken.LockToken}");
        Console.WriteLine($"Error Code: {failedLockToken.Error.Code}");
        Console.WriteLine($"Error Description: {failedLockToken.Error.Message}");
    }

    Console.WriteLine($"Success count for Acknowledge: {acknowledgeResult.SucceededLockTokens.Count}");
    foreach (string lockToken in acknowledgeResult.SucceededLockTokens)
    {
        Console.WriteLine($"Lock Token: {lockToken}");
    }
}

if (toReject.Count > 0)
{
    RejectResult rejectResult = await receiverClient.RejectAsync(toReject);

    // Inspect the Reject result
    Console.WriteLine($"Failed count for Reject: {rejectResult.FailedLockTokens.Count}");
    foreach (FailedLockToken failedLockToken in rejectResult.FailedLockTokens)
    {
        Console.WriteLine($"Lock Token: {failedLockToken.LockToken}");
        Console.WriteLine($"Error Code: {failedLockToken.Error.Code}");
        Console.WriteLine($"Error Description: {failedLockToken.Error.Message}");
    }

    Console.WriteLine($"Success count for Reject: {rejectResult.SucceededLockTokens.Count}");
    foreach (string lockToken in rejectResult.SucceededLockTokens)
    {
        Console.WriteLine($"Lock Token: {lockToken}");
    }
}

Create DataModel class

Add the following code to the end of your file if you’d like to use the DataModel shown in the code samples.

public class DataModel
{
    public string Name { get; set; }
    public int Age { get; set; }
}

More language samples

For samples and getting started guides, select a language-specific link:

Support channels

You can share feedback, make feature requests, ask questions, or report bugs through issues on any of our GitHub repositories.

Author

Ronnie Geraghty
Product Manager

Product Manager for the Azure SDK covering C/C++, Rust, Event Grid, Event Hubs, Schema Registry and Service Bus.

0 comments

Discussion are closed.