Getting Started with Azure Event Hubs: Sending and Receiving Messages
This guide will walk you through the fundamental steps of sending and receiving messages using Azure Event Hubs, a highly scalable data streaming platform.
Before you begin, ensure you have an Azure subscription and an Event Hubs namespace created.
1. Setting Up Your Environment
You'll need the Azure CLI or Azure PowerShell, and the appropriate SDK for your programming language.
Using .NET SDK (Example)
Install the necessary NuGet packages:
dotnet add package Azure.Messaging.EventHubs
dotnet add package Azure.Messaging.EventHubs.Processor
2. Connecting to Your Event Hub
You'll need your Event Hub's connection string. You can find this in the Azure portal under your Event Hub namespace's "Shared access policies".
Treat your connection string as sensitive information. Do not expose it directly in client-side code.
3. Sending Messages
Create an EventHubProducerClient and send events. Each event can contain a payload of any data type.
using Azure.Messaging.EventHubs;
using System;
using System.Text;
using System.Threading.Tasks;
// Replace with your actual connection string and event hub name
string connectionString = Environment.GetEnvironmentVariable("EVENTHUB_CONNECTION_STRING");
string eventHubName = "your-event-hub-name";
await using var producerClient = new EventHubProducerClient(connectionString, eventHubName);
using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();
for (int i = 1; i <= 5; i++)
{
var message = $"Message {i}";
if (!eventBatch.TryAddMessage(new EventData(Encoding.UTF8.GetBytes(message))))
{
// If the batch is full, send it and start a new one
throw new Exception($"The batch is full. Failed to add message {i}.");
}
}
// Send the batch of events
await producerClient.SendAsync(eventBatch);
Console.WriteLine($"Sent a batch of {eventBatch.Count} messages.");
4. Receiving Messages
Use the EventHubConsumerClient to process events. You can consume from a specific consumer group.
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Consumer;
using System;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
// Replace with your actual connection string and event hub name
string connectionString = Environment.GetEnvironmentVariable("EVENTHUB_CONNECTION_STRING");
string eventHubName = "your-event-hub-name";
string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName; // Or your custom consumer group
await using var consumerClient = new EventHubConsumerClient(consumerGroup, connectionString, eventHubName);
Console.WriteLine("Listening for messages...");
try
{
await foreach (PartitionEvent @event in consumerClient.ReadEventsAsync())
{
Console.WriteLine($"\nReceived event: {Encoding.UTF8.GetString(@event.Data.EventBody.ToArray())}");
Console.WriteLine($" Enqueued Time: {@event.Data.EnqueuedTime.ToLocalTime()}");
Console.WriteLine($" Partition Id: {@event.Partition.Id}");
}
}
catch (TaskCanceledException)
{
// The operation was cancelled, likely due to timeout or explicit cancellation
Console.WriteLine("Message consumption task was cancelled.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
Next Steps
- Explore partitioning to understand how to scale your throughput.
- Learn about real-time data streaming scenarios.
- Investigate the consumer group concept for parallel processing.