Azure Event Hubs

Getting Started: Understanding Connection Strings

What are Connection Strings?

Connection strings are the credentials your applications use to authenticate with Azure Event Hubs. They contain essential information like the endpoint, shared access key name, and the primary or secondary key. Think of them as the "keys to the kingdom" for your Event Hubs namespace.

Where to Find Your Connection String

You can find your Event Hubs connection string in the Azure portal:

  1. Navigate to your Event Hubs namespace.
  2. In the left-hand menu, under Settings, select Shared access policies.
  3. Click on an existing policy (e.g., RootManageSharedAccessKey) or create a new one with appropriate permissions.
  4. The connection string will be displayed on the policy details page. You can copy the Primary Connection String or Secondary Connection String.

Connection String Components

A typical Event Hubs connection string looks like this:

Endpoint=sb://your-namespace.servicebus.windows.net/;SharedAccessKeyName=your-key-name;SharedAccessKey=your-secret-key

Let's break down the components:

Using Connection Strings in Your Code

You'll typically configure your application to read the connection string from environment variables or a configuration file. Here's a conceptual example of how you might use it with an Event Hubs client library (syntax may vary by language):

// Example in C# using Azure.Messaging.EventHubs
using Azure.Messaging.EventHubs;

string connectionString = Environment.GetEnvironmentVariable("EVENTHUB_CONNECTION_STRING");
string eventHubName = "your-eventhub-name";

await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName))
{
    // Send events...
}
            
Security Best Practice: Never hardcode connection strings directly into your source code. Always use secure methods like environment variables, Azure Key Vault, or managed identities to manage your credentials.

Primary vs. Secondary Keys

Your shared access policies come with both a primary and a secondary key. It's a good security practice to use the secondary key for your application and keep the primary key as a backup. If you ever need to rotate your keys, you can update your application to use the primary key while you regenerate the secondary key, minimizing downtime.

Next Steps

Now that you understand connection strings, you're ready to start sending and receiving events. Explore the API Reference for detailed information on client libraries and explore how to send your first events.