Use Azure SignalR Service (original) (raw)

This article shows you how to use SDK in your app server side to connect to SignalR Service when you are using SignalR in your app server.

Important

Raw connection strings appear in this article for demonstration purposes only.

A connection string includes the authorization information required for your application to access Azure SignalR Service. The access key inside the connection string is similar to a root password for your service. In production environments, always protect your access keys. Use Azure Key Vault to manage and rotate your keys securely and secure your connection string using Microsoft Entra ID and authorize access with Microsoft Entra ID.

Avoid distributing access keys to other users, hard-coding them, or saving them anywhere in plain text that is accessible to others. Rotate your keys if you believe they may have been compromised.

Create an Azure SignalR Service instance

Follow Quickstart: Use an ARM template to deploy Azure SignalR to create a SignalR service instance.

For ASP.NET Core SignalR

Install the SDK

Run the command to install SignalR Service SDK to your ASP.NET Core project.

dotnet add package Microsoft.Azure.SignalR

In your Startup class, use SignalR Service SDK as the following code snippet.

public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR()
            .AddAzureSignalR();
}

public void Configure(IApplicationBuilder app)
{
    app.UseEndpoints(routes =>
    {
        routes.MapHub<YourHubClass>("/path_for_your_hub");
    });
}

Configure connection string

Raw connection strings appear in this article for demonstration purposes only. In production environments, always protect your access keys. Use Azure Key Vault to manage and rotate your keys securely and secure your connection string using Microsoft Entra ID and authorize access with Microsoft Entra ID.

There are two approaches to configure SignalR Service's connection string in your application.

services.AddSignalR()  
        .AddAzureSignalR("<replace with your connection string>");  

or

services.AddSignalR()  
        .AddAzureSignalR(options => options.ConnectionString = "<replace with your connection string>");  

Configure options

There are a few options you can customize when using Azure SignalR Service SDK.

ConnectionString

InitialHubServerConnectionCount

MaxHubServerConnectionCount

ApplicationName

ClaimsProvider

AccessTokenLifetime

AccessTokenAlgorithm

ServerStickyMode

GracefulShutdown

GracefulShutdown.Mode
GracefulShutdown.Timeout

ServiceScaleTimeout

MaxPollIntervalInSeconds

TransportTypeDetector

AllowStatefulReconnects

Sample

You can configure above options like the following sample code.

services.AddSignalR()
        .AddAzureSignalR(options =>
            {
                options.InitialHubServerConnectionCount = 10;
                options.AccessTokenLifetime = TimeSpan.FromDays(1);
                options.ClaimsProvider = context => context.User.Claims;

                options.GracefulShutdown.Mode = GracefulShutdownMode.WaitForClientsClose;
                options.GracefulShutdown.Timeout = TimeSpan.FromSeconds(10);
                options.TransportTypeDetector = httpContext => AspNetCore.Http.Connections.HttpTransportType.WebSockets | AspNetCore.Http.Connections.HttpTransportType.LongPolling;
            });

For the legacy ASP.NET SignalR

Note

If it is your first time trying SignalR, we recommend you use the ASP.NET Core SignalR, it is simpler, more reliable, and easier to use.

Install the SDK

Install SignalR Service SDK to your ASP.NET project with Package Manager Console:

Install-Package Microsoft.Azure.SignalR.AspNet

In your Startup class, use SignalR Service SDK as the following code snippet, replace MapSignalR() to MapAzureSignalR({your_applicationName}). Replace {YourApplicationName} to the name of your application, this is the unique name to distinguish this application with your other applications. You can use this.GetType().FullName as the value.

public void Configuration(IAppBuilder app)
{
    app.MapAzureSignalR(this.GetType().FullName);
}

Configure connection string

Set the connection string in the web.config file, to the connectionStrings section:

<configuration>
    <connectionStrings>
        <add name="Azure:SignalR:ConnectionString" connectionString="Endpoint=...;AccessKey=..."/>
    </connectionStrings>
    ...
</configuration>

Configure options

There are a few options you can customize when using Azure SignalR Service SDK.

ConnectionString

InitialHubServerConnectionCount

MaxHubServerConnectionCount

ApplicationName

ClaimProvider

AccessTokenLifetime

AccessTokenAlgorithm

ServerStickyMode

MaxPollIntervalInSeconds

Sample

You can configure above options like the following sample code.

app.Map("/signalr",subApp => subApp.RunAzureSignalR(this.GetType().FullName, new HubConfiguration(), options =>
{
    options.InitialHubServerConnectionCount = 1;
    options.AccessTokenLifetime = TimeSpan.FromDays(1);
    options.ClaimProvider = context => context.Authentication?.User.Claims;
}));

Scale out application server

With Azure SignalR Service, persistent connections are offloaded from application server so that you can focus on implementing your business logic in hub classes. But you still need to scale out application servers for better performance when handling massive client connections. Below are a few tips for scaling out application servers.

Next steps

In this article, you learn how to use SignalR Service in your applications. Check the following articles to learn more about SignalR Service.