Connect to Azure Event Hubs

This guide will walk you through the essential steps to establish a connection to your Azure Event Hubs instance. We'll cover obtaining connection strings and using common SDKs.

Prerequisites

1. Obtain Connection String

The connection string is your primary credential for accessing your Event Hubs. You can find it in the Azure portal.

1

Navigate to your Event Hubs namespace in the Azure portal.

2

In the left-hand menu, under Settings, select Shared access policies.

3

You will see a list of policies. The RootManageSharedAccessKey is a default policy with full access. Click on it.

4

Copy the Primary Connection String. Keep this string secure, as it grants access to your Event Hubs.

Security Note: Treat your connection string like a password. Do not expose it in client-side code or commit it to source control. Use environment variables or Azure Key Vault for managing secrets.

2. Connecting with SDKs

Azure provides SDKs for various programming languages to interact with Event Hubs. Here are examples for common languages.

2.1. .NET SDK

Using the Azure.Messaging.EventHubs NuGet package.

C#

using Azure.Messaging.EventHubs;
using System;
using System.Threading.Tasks;

public class EventHubConnector
{
    private const string connectionString = ""; // Replace with your connection string
    private const string eventHubName = ""; // Replace with your Event Hub name

    public static async Task Main(string[] args)
    {
        // The producer client can be created using the connection string and event hub name
        await using var producerClient = new EventHubProducerClient(connectionString, eventHubName);

        Console.WriteLine("Successfully created Event Hub producer client.");

        // You can now use producerClient to send events.
        // For receiving, you would use EventHubConsumerClient.
    }
}
            

2.2. Python SDK

Using the azure-eventhubs package.

Python

from azure.eventhub import EventHubClient, EventPosition
import os

# Replace with your connection string and event hub name
connection_str = os.environ.get("") # Example using env var
event_hub_name = ""

if not connection_str:
    raise ValueError("Environment variable 'AZURE_EVENTHUBS_CONNECTION_STRING' is not set.")

client = EventHubClient.from_connection_string(connection_str, event_hub_name)

print("Successfully created Event Hub client.")

# For sending, you'd use client.create_producer()
# For receiving, you'd use client.create_consumer()
            

2.3. JavaScript/TypeScript SDK

Using the @azure/event-hubs npm package.

TypeScript

import { EventHubProducerClient, EventHubConsumerClient } from "@azure/event-hubs";
import { DefaultAzureCredential } from "@azure/identity";

const connectionString = process.env["EVENTHUB_CONNECTION_STRING"]; // Recommended to use environment variables
const eventHubName = ""; // Replace with your Event Hub name

if (!connectionString) {
    throw new Error("Environment variable EVENTHUB_CONNECTION_STRING is not set.");
}

// For producing events
const producerClient = new EventHubProducerClient(connectionString, eventHubName);
console.log("Successfully created Event Hub producer client.");

// For consuming events
// const consumerClient = new EventHubConsumerClient(connectionString, eventHubName);
// console.log("Successfully created Event Hub consumer client.");

// Alternatively, using Azure Identity for authentication (e.g., Managed Identity, Service Principal)
// const credential = new DefaultAzureCredential();
// const producerClientWithIdentity = new EventHubProducerClient("", eventHubName, credential);

            

Key Components for Connection:

  • Connection String: Contains endpoint, shared access key name, and key.
  • Event Hub Name: The specific hub you want to connect to within the namespace.
  • SDKs: Libraries that abstract the complexities of the Event Hubs protocol.

Once you have successfully instantiated a client using your connection details, you are ready to start sending or receiving messages. Refer to the Send & Receive Messages guide for the next steps.