Aspire

Connect to Azure Web PubSub

Azure Web PubSub logo

This page describes how consuming apps connect to an Azure Web PubSub resource that’s already modeled in your AppHost. For the AppHost API surface — adding a Web PubSub resource, hubs, event handlers, the local emulator, role assignments, and more — see Azure Web PubSub Hosting integration.

When you reference an Azure Web PubSub resource from your AppHost, Aspire injects the connection information into the consuming app as environment variables. Your app can either read those environment variables directly — the pattern works the same from any language — or, in C#, use the Aspire Azure Web PubSub client integration for automatic dependency injection, health checks, and telemetry.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called web-pubsub becomes WEB_PUBSUB_URI, and the HubName property of a hub called chat becomes CHAT_HUBNAME.

The Azure Web PubSub resource exposes the following connection properties:

Property NameDescription
UriThe service endpoint. In Azure, this is the HTTPS endpoint with the format https://{resource-name}.webpubsub.azure.com. When the resource runs with the emulator, this is the emulator’s local HTTP endpoint.

Example:

WEB_PUBSUB_URI: https://webpubsub-abc123.webpubsub.azure.com

When you reference a hub resource (added with AddHub), the hub inherits all properties from its parent Web PubSub service and adds:

Property NameDescription
HubNameThe name of the Web PubSub hub

Example:

CHAT_URI: https://webpubsub-abc123.webpubsub.azure.com
CHAT_HUBNAME: chat

Azure Web PubSub clients accept two connection formats:

  • Service endpoint (recommended): Uses the endpoint URL with a managed identity / default credential. This is the format Aspire injects by default.

    https://{account_name}.webpubsub.azure.com
  • Full connection string (includes access key): Combines the Endpoint and AccessKey components. Use this only when managed identity isn’t available.

    Endpoint=https://{account_name}.webpubsub.azure.com;AccessKey={account_key}

When the AppHost calls RunAsEmulator, the emulator doesn’t use managed identity. Instead, Aspire provides a connection string that includes the emulator’s HTTP endpoint and access key:

Endpoint=http://localhost:{port};AccessKey={emulator_access_key};Version=1.0

When you reference a hub, the connection string also includes the hub name, for example ...;Version=1.0;Hub=chat.

  • C#: The Aspire client integration reads this connection string, including the hub name, when you pass the referenced resource name as the connectionName, for example builder.AddAzureWebPubSubServiceClient("chat").

  • Other languages: Create the service client from the connection string rather than from Uri and a token credential. Use the HubName property for the hub name. The JavaScript @azure/web-pubsub client requires allowInsecureConnection: true to connect to the emulator’s HTTP endpoint:

    Connect to the emulator
    import { WebPubSubServiceClient } from '@azure/web-pubsub';
    const client = new WebPubSubServiceClient(
    process.env.ConnectionStrings__chat!,
    process.env.CHAT_HUBNAME!,
    { allowInsecureConnection: true });

The emulator is for local development only. Publishing your app still provisions Azure Web PubSub, where the managed identity examples on this page apply.

Pick the language your consuming app is written in. Each example assumes your AppHost adds an Azure Web PubSub resource named web-pubsub and references it from the consuming app.

For C# apps, the recommended approach is the Aspire Azure Web PubSub client integration. It registers a WebPubSubServiceClient through dependency injection and adds health checks and telemetry automatically. If you’d rather read environment variables directly, see the Read environment variables section at the end of this tab.

Install the 📦 Aspire.Azure.Messaging.WebPubSub NuGet package in the client-consuming project:

.NET CLI — Add Aspire.Azure.Messaging.WebPubSub package
dotnet add package Aspire.Azure.Messaging.WebPubSub

In Program.cs, call AddAzureWebPubSubServiceClient on your IHostApplicationBuilder to register a WebPubSubServiceClient:

Program.cs
builder.AddAzureWebPubSubServiceClient(
connectionName: "web-pubsub",
settings => settings.HubName = "messages");

Resolve the client through dependency injection:

ExampleService.cs
public class ExampleService(WebPubSubServiceClient client)
{
// Use client...
}

To register multiple WebPubSubServiceClient instances for different hubs, use AddKeyedAzureWebPubSubServiceClient:

Program.cs
builder.AddKeyedAzureWebPubSubServiceClient(name: "messages");
builder.AddKeyedAzureWebPubSubServiceClient(name: "commands");

Then resolve each client by key:

ExampleService.cs
public class ExampleService(
[FromKeyedServices("messages")] WebPubSubServiceClient messagesClient,
[FromKeyedServices("commands")] WebPubSubServiceClient commandsClient)
{
// Use clients...
}

The Aspire Azure Web PubSub client integration offers multiple ways to provide configuration.

Connection strings. When using a connection string from the ConnectionStrings configuration section, pass the connection name to AddAzureWebPubSubServiceClient. Two formats are supported:

  • Service endpoint (recommended):

    appsettings.json
    {
    "ConnectionStrings": {
    "web-pubsub": "https://{account_name}.webpubsub.azure.com"
    }
    }
  • Full connection string with access key:

    appsettings.json
    {
    "ConnectionStrings": {
    "web-pubsub": "Endpoint=https://{account_name}.webpubsub.azure.com;AccessKey={account_key}"
    }
    }

Configuration providers. The client integration supports Microsoft.Extensions.Configuration. It loads AzureMessagingWebPubSubSettings from appsettings.json using the Aspire:Azure:Messaging:WebPubSub key:

appsettings.json
{
"Aspire": {
"Azure": {
"Messaging": {
"WebPubSub": {
"HubName": "messages",
"DisableHealthChecks": false
}
}
}
}
}

Inline delegates. Pass an Action<AzureMessagingWebPubSubSettings> to configure settings inline:

Program.cs
builder.AddAzureWebPubSubServiceClient(
"web-pubsub",
settings => settings.DisableHealthChecks = true);

Aspire client integrations enable health checks by default. The Azure Web PubSub client integration registers a health check that verifies the service is reachable. The health check is wired into the /health HTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.

The Aspire Azure Web PubSub client integration automatically configures logging, tracing, and metrics through OpenTelemetry.

Logging categories:

  • Azure
  • Azure.Core
  • Azure.Identity
  • Azure.Messaging.WebPubSub

Tracing activities:

  • Azure.Messaging.WebPubSub.*

Metrics: The Azure Web PubSub integration currently doesn’t support metrics by default due to limitations in the Azure SDK for .NET.

If you prefer not to use the Aspire client integration, you can read the Aspire-injected endpoint URI from the environment and create a WebPubSubServiceClient directly:

Program.cs
using Azure.Messaging.WebPubSub;
using Azure.Identity;
var endpoint = new Uri(Environment.GetEnvironmentVariable("WEB_PUBSUB_URI")!);
var client = new WebPubSubServiceClient(endpoint, "messages", new DefaultAzureCredential());
// Use client...