This page shows supported authentication methods and clients, and shows sample code you can use to connect Azure Web PubSub to other cloud services using Service Connector. You might still be able to connect to App Configuration using other methods. This page also shows default environment variable names and values you get when you create the service connection.
Supported compute services
Service Connector can be used to connect the following compute services to Azure Web PubSub:
- Azure App Service
- Azure Container Apps
- Azure Functions
- Azure Kubernetes Service (AKS)
- Azure Spring Apps
Supported authentication types and client types
The table below shows which combinations of authentication methods and clients are supported for connecting your compute service to Azure Web PubSub using Service Connector. A “Yes” indicates that the combination is supported, while a “No” indicates that it is not supported.
Client type |
System-assigned managed identity |
User-assigned managed identity |
Secret/connection string |
Service principal |
.NET |
Yes |
Yes |
Yes |
Yes |
Java |
Yes |
Yes |
Yes |
Yes |
Node.js |
Yes |
Yes |
Yes |
Yes |
Python |
Yes |
Yes |
Yes |
Yes |
This table indicates that all combinations of client types and authentication methods in the table are supported. Other client types are not supported to connect to Azure Web PubSub using Service Connector.
Default environment variable names or application properties and sample code
Use the environment variable names and application properties listed below, according to your connection's authentication type and client type, to connect compute services to Web PubSub using .NET, Java, Node.js, or Python. For each example below, replace the placeholder texts <name>
, <client-id>
, <client-secret
, <access-key>
, and <tenant-id>
with your own resource name, client ID, client secret, access-key, and tenant ID. For more information about naming conventions, check the Service Connector internals article.
System-assigned managed identity
Default environment variable name |
Description |
Sample value |
AZURE_WEBPUBSUB_HOST |
Azure Web PubSub host |
<name>.webpubsub.azure.com |
Sample code
Refer to the steps and code below to connect to Azure Web PubSub using a system-assigned managed identity.
- Install dependencies.
dotnet add package Azure.Identity
dotnet add package Azure.Messaging.WebPubSub
- Authenticate using
Azure.Identity
and get the endpoint URL from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
using Azure.Identity;
using Azure.Messaging.WebPubSub;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// var sqlServerTokenProvider = new DefaultAzureCredential();
// For user-assigned identity.
// var sqlServerTokenProvider = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_CLIENTID");
// }
// );
// For service principal.
// var tenantId = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_CLIENTSECRET");
// var sqlServerTokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);
var endpoint = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_HOST");
// Replace "<hub>" with your hub name.
var client = new WebPubSubServiceClient(new Uri(endpoint), "<hub>", credential);
- Add the following dependencies in your pom.xml file:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-webpubsub</artifactId>
<version>1.0.0</version>
</dependency>
- Authenticate using
azure-identity
and get the endpoint URL from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned managed identity.
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// For user-assigned managed identity.
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_WEBPUBSUB_CLIENTID"))
// .build();
// For service principal.
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("<AZURE_WEBPUBSUB_CLIENTID>"))
// .clientSecret(System.getenv("<AZURE_WEBPUBSUB_CLIENTSECRET>"))
// .tenantId(System.getenv("<AZURE_WEBPUBSUB_TENANTID>"))
// .build();
String endpoint = System.getenv("AZURE_WEBPUBSUB_HOST");
// Replace "<hub>" with your hub name.
WebPubSubServiceClient client = new WebPubSubServiceClientBuilder()
.endpoint(endpoint)
.credential(credential)
.hub("<hub>")
.buildClient();
- Install dependencies.
python -m pip install azure-identity
python -m pip install azure-messaging-webpubsubservice
- Authenticate using
azure-identity
and get the endpoint URL from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
from azure.messaging.webpubsubservice import WebPubSubServiceClient
# Uncomment the following lines corresponding to the authentication type you want to use.
# For system-assigned managed identity.
# cred = ManagedIdentityCredential()
# For user-assigned managed identity.
# managed_identity_client_id = os.getenv('AZURE_WEBPUBSUB_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# For service principal.
# tenant_id = os.getenv('AZURE_WEBPUBSUB_TENANTID')
# client_id = os.getenv('AZURE_WEBPUBSUB_CLIENTID')
# client_secret = os.getenv('AZURE_WEBPUBSUB_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
endpoint = os.getenv("AZURE_WEBPUBSUB_HOST")
# Replace "<hub>" with your hub name.
client = WebPubSubServiceClient(hub="<hub>", endpoint=endpoint, credential=cred)
- Install dependencies.
npm install @azure/web-pubsub
npm install --save @azure/identity
- Authenticate using
azure-identity
and get the endpoint URL from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
const { DefaultAzureCredential,ClientSecretCredential } = require("@azure/identity");
const { WebPubSubServiceClient } = require("@azure/web-pubsub");
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// const credential = new DefaultAzureCredential();
// For user-assigned identity.
// const clientId = process.env.AZURE_WEBPUBSUB_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// For service principal.
// const tenantId = process.env.AZURE_WEBPUBSUB_TENANTID;
// const clientId = process.env.AZURE_WEBPUBSUB_CLIENTID;
// const clientSecret = process.env.AZURE_WEBPUBSUB_CLIENTSECRET;
const endpoint = process.env.AZURE_WEBPUBSUB_HOST;
// Replace "<hub>" with your hub name.
let serviceClient = new WebPubSubServiceClient(
endpoint,
credential,
"<hub>"
);
For other languages, you can use the connection configuration properties that Service Connector sets to the environment variables to connect to Azure Web PubSub. For environment variable details, see Integrate Azure Web PubSub with Service Connector.
User-assigned managed identity
Default environment variable name |
Description |
Sample value |
AZURE_WEBPUBSUB_HOST |
Azure Web PubSub host |
<name>.webpubsub.azure.com |
AZURE_WEBPUBSUB_CLIENTID |
Azure Web PubSub client ID |
<client-id> |
Sample code
Refer to the steps and code below to connect to Azure Web PubSub using a user-assigned managed identity.
- Install dependencies.
dotnet add package Azure.Identity
dotnet add package Azure.Messaging.WebPubSub
- Authenticate using
Azure.Identity
and get the endpoint URL from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
using Azure.Identity;
using Azure.Messaging.WebPubSub;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// var sqlServerTokenProvider = new DefaultAzureCredential();
// For user-assigned identity.
// var sqlServerTokenProvider = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_CLIENTID");
// }
// );
// For service principal.
// var tenantId = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_CLIENTSECRET");
// var sqlServerTokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);
var endpoint = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_HOST");
// Replace "<hub>" with your hub name.
var client = new WebPubSubServiceClient(new Uri(endpoint), "<hub>", credential);
- Add the following dependencies in your pom.xml file:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-webpubsub</artifactId>
<version>1.0.0</version>
</dependency>
- Authenticate using
azure-identity
and get the endpoint URL from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned managed identity.
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// For user-assigned managed identity.
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_WEBPUBSUB_CLIENTID"))
// .build();
// For service principal.
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("<AZURE_WEBPUBSUB_CLIENTID>"))
// .clientSecret(System.getenv("<AZURE_WEBPUBSUB_CLIENTSECRET>"))
// .tenantId(System.getenv("<AZURE_WEBPUBSUB_TENANTID>"))
// .build();
String endpoint = System.getenv("AZURE_WEBPUBSUB_HOST");
// Replace "<hub>" with your hub name.
WebPubSubServiceClient client = new WebPubSubServiceClientBuilder()
.endpoint(endpoint)
.credential(credential)
.hub("<hub>")
.buildClient();
- Install dependencies.
python -m pip install azure-identity
python -m pip install azure-messaging-webpubsubservice
- Authenticate using
azure-identity
and get the endpoint URL from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
from azure.messaging.webpubsubservice import WebPubSubServiceClient
# Uncomment the following lines corresponding to the authentication type you want to use.
# For system-assigned managed identity.
# cred = ManagedIdentityCredential()
# For user-assigned managed identity.
# managed_identity_client_id = os.getenv('AZURE_WEBPUBSUB_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# For service principal.
# tenant_id = os.getenv('AZURE_WEBPUBSUB_TENANTID')
# client_id = os.getenv('AZURE_WEBPUBSUB_CLIENTID')
# client_secret = os.getenv('AZURE_WEBPUBSUB_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
endpoint = os.getenv("AZURE_WEBPUBSUB_HOST")
# Replace "<hub>" with your hub name.
client = WebPubSubServiceClient(hub="<hub>", endpoint=endpoint, credential=cred)
- Install dependencies.
npm install @azure/web-pubsub
npm install --save @azure/identity
- Authenticate using
azure-identity
and get the endpoint URL from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
const { DefaultAzureCredential,ClientSecretCredential } = require("@azure/identity");
const { WebPubSubServiceClient } = require("@azure/web-pubsub");
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// const credential = new DefaultAzureCredential();
// For user-assigned identity.
// const clientId = process.env.AZURE_WEBPUBSUB_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// For service principal.
// const tenantId = process.env.AZURE_WEBPUBSUB_TENANTID;
// const clientId = process.env.AZURE_WEBPUBSUB_CLIENTID;
// const clientSecret = process.env.AZURE_WEBPUBSUB_CLIENTSECRET;
const endpoint = process.env.AZURE_WEBPUBSUB_HOST;
// Replace "<hub>" with your hub name.
let serviceClient = new WebPubSubServiceClient(
endpoint,
credential,
"<hub>"
);
For other languages, you can use the connection configuration properties that Service Connector sets to the environment variables to connect to Azure Web PubSub. For environment variable details, see Integrate Azure Web PubSub with Service Connector.
Connection string
Warning
Microsoft recommends that you use the most secure authentication flow available. The authentication flow described in this procedure requires a very high degree of trust in the application, and carries risks that are not present in other flows. You should only use this flow when other more secure flows, such as managed identities, aren't viable.
Default environment variable name |
Description |
Sample value |
AZURE_WEBPUBSUB_CONNECTIONSTRING |
Azure Web PubSub connection string |
Endpoint=https://<name>.webpubsub.azure.com;AccessKey=<access-key>;Version=1.0; |
Sample code
Refer to the steps and code below to connect to Azure Web PubSub using a connection string.
Install dependencies.
dotnet add package Azure.Messaging.WebPubSub
Get the connection string from the environment variable added by Service Connector.
using Azure.Messaging.WebPubSub;
string connectionString = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_CONNECTIONSTRING");
// Replace "<hub>" with your hub name.
var serviceClient = new WebPubSubServiceClient(connectionString, "<hub>");
- Add the following dependencies in your pom.xml file:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-webpubsub</artifactId>
<version>1.0.0</version>
</dependency>
- Get the connection string from the environment variable added by Service Connector.
String connectionString = System.getenv("AZURE_WEBPUBSUB_CONNECTIONSTRING");
// Replace "<hub>" with your hub name.
WebPubSubServiceClient webPubSubServiceClient = new WebPubSubServiceClientBuilder()
.connectionString(connectionString)
.hub("<hub>")
.buildClient();
- Install dependencies.
python -m pip install azure-messaging-webpubsubservice
- Get the connection string from the environment variable added by Service Connector.
from azure.messaging.webpubsubservice import WebPubSubServiceClient
connection_string = os.getenv('AZURE_WEBPUBSUB_CONNECTIONSTRING')
# Replace "<hub>" with your hub name.
service = WebPubSubServiceClient.from_connection_string(connection_string=connection_string, hub='<hub>')
- Install dependencies.
npm install @azure/web-pubsub
- Get the connection string from the environment variable added by Service Connector.
const { WebPubSubServiceClient } = require("@azure/web-pubsub");
const ConnectionString = process.env.AZURE_WEBPUBSUB_CONNECTIONSTRING;
// Replace "<hub>" with your hub name.
const serviceClient = new WebPubSubServiceClient(ConnectionString, "<hubName>");
For other languages, you can use the connection configuration properties that Service Connector sets to the environment variables to connect to Azure Web PubSub. For environment variable details, see Integrate Azure Web PubSub with Service Connector.
Service principal
Default environment variable name |
Description |
Sample value |
AZURE_WEBPUBSUB_HOST |
Azure Web PubSub host |
<name>.webpubsub.azure.com |
AZURE_WEBPUBSUB_CLIENTID |
Azure Web PubSub client ID |
<client-id> |
AZURE_WEBPUBSUB_CLIENTSECRET |
Azure Web PubSub client secret |
<client-secret> |
AZURE_WEBPUBSUB_TENANTID |
Azure Web PubSub tenant ID |
<tenant-id> |
Sample code
Refer to the steps and code below to connect to Azure Web PubSub using a service principal.
- Install dependencies.
dotnet add package Azure.Identity
dotnet add package Azure.Messaging.WebPubSub
- Authenticate using
Azure.Identity
and get the endpoint URL from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
using Azure.Identity;
using Azure.Messaging.WebPubSub;
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// var sqlServerTokenProvider = new DefaultAzureCredential();
// For user-assigned identity.
// var sqlServerTokenProvider = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_CLIENTID");
// }
// );
// For service principal.
// var tenantId = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_CLIENTSECRET");
// var sqlServerTokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);
var endpoint = Environment.GetEnvironmentVariable("AZURE_WEBPUBSUB_HOST");
// Replace "<hub>" with your hub name.
var client = new WebPubSubServiceClient(new Uri(endpoint), "<hub>", credential);
- Add the following dependencies in your pom.xml file:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-webpubsub</artifactId>
<version>1.0.0</version>
</dependency>
- Authenticate using
azure-identity
and get the endpoint URL from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned managed identity.
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// For user-assigned managed identity.
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_WEBPUBSUB_CLIENTID"))
// .build();
// For service principal.
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("<AZURE_WEBPUBSUB_CLIENTID>"))
// .clientSecret(System.getenv("<AZURE_WEBPUBSUB_CLIENTSECRET>"))
// .tenantId(System.getenv("<AZURE_WEBPUBSUB_TENANTID>"))
// .build();
String endpoint = System.getenv("AZURE_WEBPUBSUB_HOST");
// Replace "<hub>" with your hub name.
WebPubSubServiceClient client = new WebPubSubServiceClientBuilder()
.endpoint(endpoint)
.credential(credential)
.hub("<hub>")
.buildClient();
- Install dependencies.
python -m pip install azure-identity
python -m pip install azure-messaging-webpubsubservice
- Authenticate using
azure-identity
and get the endpoint URL from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
from azure.messaging.webpubsubservice import WebPubSubServiceClient
# Uncomment the following lines corresponding to the authentication type you want to use.
# For system-assigned managed identity.
# cred = ManagedIdentityCredential()
# For user-assigned managed identity.
# managed_identity_client_id = os.getenv('AZURE_WEBPUBSUB_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# For service principal.
# tenant_id = os.getenv('AZURE_WEBPUBSUB_TENANTID')
# client_id = os.getenv('AZURE_WEBPUBSUB_CLIENTID')
# client_secret = os.getenv('AZURE_WEBPUBSUB_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
endpoint = os.getenv("AZURE_WEBPUBSUB_HOST")
# Replace "<hub>" with your hub name.
client = WebPubSubServiceClient(hub="<hub>", endpoint=endpoint, credential=cred)
- Install dependencies.
npm install @azure/web-pubsub
npm install --save @azure/identity
- Authenticate using
azure-identity
and get the endpoint URL from the environment variable added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
const { DefaultAzureCredential,ClientSecretCredential } = require("@azure/identity");
const { WebPubSubServiceClient } = require("@azure/web-pubsub");
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// const credential = new DefaultAzureCredential();
// For user-assigned identity.
// const clientId = process.env.AZURE_WEBPUBSUB_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// For service principal.
// const tenantId = process.env.AZURE_WEBPUBSUB_TENANTID;
// const clientId = process.env.AZURE_WEBPUBSUB_CLIENTID;
// const clientSecret = process.env.AZURE_WEBPUBSUB_CLIENTSECRET;
const endpoint = process.env.AZURE_WEBPUBSUB_HOST;
// Replace "<hub>" with your hub name.
let serviceClient = new WebPubSubServiceClient(
endpoint,
credential,
"<hub>"
);
For other languages, you can use the connection configuration properties that Service Connector sets to the environment variables to connect to Azure Web PubSub. For environment variable details, see Integrate Azure Web PubSub with Service Connector.
Next steps
Read the article listed below to learn more about Service Connector.