Send and Receive Messages with Azure Event Hubs
This tutorial guides you through the process of sending messages to and receiving messages from an Azure Event Hub. We'll use C# for this example, but the concepts are applicable to other SDKs.
- An Azure subscription.
- An Azure Event Hubs namespace and an Event Hub created within it.
- The Azure CLI installed or an Azure account with permissions to manage Event Hubs.
- .NET SDK installed (for C# examples).
1. Setting up your Environment
Before you begin, ensure you have your Event Hub connection string. You can find this in the Azure portal under your Event Hubs namespace's "Shared access policies".
You'll need to install the Azure Event Hubs SDK for .NET. You can do this via NuGet Package Manager:
dotnet add package Azure.Messaging.EventHubs
2. Sending Messages
To send messages, we'll use the EventHubProducerClient. This client allows you to send batches of events to your Event Hub.
Sender Application (C#)
Create a new C# console application and add the following code:
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
class EventHubSender
{
// Replace with your actual connection string and event hub name
private const string connectionString = "";
private const string eventHubName = "";
public static async Task Main(string[] args)
{
await SendEventsToEventHub(10); // Send 10 events
Console.WriteLine("Events sent successfully.");
}
static async Task SendEventsToEventHub(int numberOfEvents)
{
await using var producer = new EventHubProducerClient(connectionString, eventHubName);
using EventDataBatch eventBatch = await producer.CreateBatchAsync();
for (int i = 1; i <= numberOfEvents; i++)
{
var eventBody = $"Message {i}";
if (!eventBatch.TryAddMessage(new EventData(Encoding.UTF8.GetBytes(eventBody))))
{
// The batch is full, send it and create a new one
await producer.SendAsync(eventBatch);
Console.WriteLine($"Sent a batch of {eventBatch.Count} events.");
// Re-create the batch after sending
// eventBatch = await producer.CreateBatchAsync(); // This line is not needed as TryAddMessage handles it internally if batch is full
}
}
// Send the last batch if it's not empty
if (eventBatch.Count > 0)
{
await producer.SendAsync(eventBatch);
Console.WriteLine($"Sent a final batch of {eventBatch.Count} events.");
}
}
}
Important: Remember to replace <YOUR_EVENT_HUB_CONNECTION_STRING> and <YOUR_EVENT_HUB_NAME> with your actual credentials.
TryAddMessage method will return false if the batch reaches its size limit, prompting you to send the current batch and start a new one.
3. Receiving Messages
To receive messages, we use the EventHubConsumerClient. This client allows you to subscribe to a specific consumer group and process events as they arrive.
Receiver Application (C#)
Create another C# console application and add the following code:
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Consumer;
using System;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
class EventHubReceiver
{
// Replace with your actual connection string and event hub name
private const string connectionString = "";
private const string eventHubName = "";
private const string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName; // Or your custom consumer group
public static async Task Main(string[] args)
{
await ProcessEvents(default); // Start processing events
}
static async Task ProcessEvents(CancellationToken cancellationToken)
{
await using var consumer = new EventHubConsumerClient(consumerGroup, connectionString, eventHubName);
Console.WriteLine("Listening for events...");
await foreach (PartitionEvent partitionEvent in consumer.ReadEventsAsync(cancellationToken))
{
Console.WriteLine($"\nReceived event: '{Encoding.UTF8.GetString(partitionEvent.Data.EventBody.ToArray())}'");
Console.WriteLine($"\tSequence number: {partitionEvent.Data.SequenceNumber}");
Console.WriteLine($"\tOffset: {partitionEvent.Data.Offset}");
Console.WriteLine($"\tPartition ID: {partitionEvent.Partition.Id}");
}
}
}
Important: Ensure the consumerGroup variable matches a valid consumer group in your Event Hub. The default is $Default.
4. Running the Applications
-
Open two separate terminal windows or command prompts.
-
In the first terminal, navigate to your sender application's directory and run:
dotnet run -
In the second terminal, navigate to your receiver application's directory and run:
dotnet run
You should see the sender application confirm that events have been sent, and the receiver application will display the messages as they are received. The receiver will continue to listen for new messages until you stop the application.
Conclusion
Congratulations! You've successfully learned how to send and receive messages using Azure Event Hubs with C#. This fundamental knowledge is key to building robust event-driven architectures on Azure.
Explore further by learning about:
- Partitioning strategies for better throughput and ordering.
- Scaling your Event Hubs capacity.
- Using different consumer groups for distinct processing needs.