How to send events from Azure SignalR Service to Event Grid
Azure Event Grid is a fully managed event routing service that provides uniform event consumption using a pub-sub model. In this guide, you use the Azure CLI to create an Azure SignalR Service, subscribe to connection events, then deploy a sample web application to receive the events. Finally, you can connect and disconnect and see the event payload in the sample application.
If you don't have an Azure subscription, create an Azure free account before you begin.
Prerequisites
Use the Bash environment in Azure Cloud Shell. For more information, see Quickstart for Bash in Azure Cloud Shell.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
- The Azure CLI commands in this article are formatted for the Bash shell. If you're using a different shell like PowerShell or Command Prompt, you may need to adjust line continuation characters or variable assignment lines accordingly. This article uses variables to minimize the amount of command editing required.
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 Web PubSub 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.
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 a resource group
An Azure resource group is a logical container in which you deploy and manage your Azure resources. The command az group create creates a resource group named myResourceGroup in the eastus region. If you want to use a different name for your resource group, set RESOURCE_GROUP_NAME
to a different value.
RESOURCE_GROUP_NAME=myResourceGroup
az group create --name $RESOURCE_GROUP_NAME --location eastus
Create a SignalR Service
Next, deploy an Azure Signals Service into the resource group with the following commands.
SIGNALR_NAME=SignalRTestSvc
az signalr create --resource-group $RESOURCE_GROUP_NAME --name $SIGNALR_NAME --sku Free_F1
Once the SignalR Service has been created, the Azure CLI returns output similar to the following example:
{
"externalIp": "13.76.156.152",
"hostName": "clitest.servicedev.signalr.net",
"hostNamePrefix": "clitest",
"id": "/subscriptions/28cf13e2-c598-4aa9-b8c8-098441f0827a/resourceGroups/clitest1/providers/Microsoft.SignalRService/SignalR/clitest",
"location": "southeastasia",
"name": "clitest",
"provisioningState": "Succeeded",
"publicPort": 443,
"resourceGroup": "clitest1",
"serverPort": 443,
"sku": {
"capacity": 1,
"family": null,
"name": "Free_F1",
"size": "F1",
"tier": "Free"
},
"tags": null,
"type": "Microsoft.SignalRService/SignalR",
"version": "1.0"
}
Create an event endpoint
In this section, you use a Resource Manager template located in a GitHub repository to deploy a prebuilt sample web application to Azure App Service. Later, you subscribe to your registry's Event Grid events and specify this app as the endpoint to which the events are sent.
To deploy the sample app, set SITE_NAME
to a unique name for your web app, and execute the following commands. The site name must be unique within Azure because it forms part of the fully qualified domain name (FQDN) of the web app. In a later section, you navigate to the app's FQDN in a web browser to view your registry's events.
SITE_NAME=<your-site-name>
az deployment group create \
--resource-group $RESOURCE_GROUP_NAME \
--template-uri "https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/master/azuredeploy.json" \
--parameters siteName=$SITE_NAME hostingPlanName=$SITE_NAME-plan
Once the deployment succeeds (it might take a few minutes), open your browser, and then go to your web app to make sure it's running:
http://<your-site-name>.azurewebsites.net
Enable the Event Grid resource provider
If you haven't previously used Event Grid in your Azure subscription, you might need to register the Event Grid resource provider. Run the following command to register the provider:
az provider register --namespace Microsoft.EventGrid
It might take a moment for the registration to finish. To check the status, run the following command:
az provider show --namespace Microsoft.EventGrid --query "registrationState"
When
registrationState
isRegistered
, you're ready to continue.
Subscribe to registry events
In Event Grid, you subscribe to a topic to tell it which events you want to track, and where to send them. The command az eventgrid event-subscription create
subscribes to the Azure SignalR Service you created and specifies your web app's URL as the endpoint to which it should send events. The environment variables you populated in earlier sections are reused here, so no edits are required.
SIGNALR_SERVICE_ID=$(az signalr show --resource-group $RESOURCE_GROUP_NAME --name $SIGNALR_NAME --query id --output tsv)
APP_ENDPOINT=https://$SITE_NAME.azurewebsites.net/api/updates
az eventgrid event-subscription create \
--name event-sub-signalr \
--source-resource-id $SIGNALR_SERVICE_ID \
--endpoint $APP_ENDPOINT
When the subscription is completed, you should see output similar to the following example:
{
"deadLetterDestination": null,
"destination": {
"endpointBaseUrl": "https://$SITE_NAME.azurewebsites.net/api/updates",
"endpointType": "WebHook",
"endpointUrl": null
},
"filter": {
"includedEventTypes": [
"Microsoft.SignalRService.ClientConnectionConnected",
"Microsoft.SignalRService.ClientConnectionDisconnected"
],
"isSubjectCaseSensitive": null,
"subjectBeginsWith": "",
"subjectEndsWith": ""
},
"id": "/subscriptions/28cf13e2-c598-4aa9-b8c8-098441f0827a/resourceGroups/myResourceGroup/providers/Microsoft.SignalRService/SignalR/SignalRTestSvc/providers/Microsoft.EventGrid/eventSubscriptions/event-sub-signalr",
"labels": null,
"name": "event-sub-signalr",
"provisioningState": "Succeeded",
"resourceGroup": "myResourceGroup",
"retryPolicy": {
"eventTimeToLiveInMinutes": 1440,
"maxDeliveryAttempts": 30
},
"topic": "/subscriptions/28cf13e2-c598-4aa9-b8c8-098441f0827a/resourceGroups/myResourceGroup/providers/microsoft.signalrservice/signalr/SignalRTestSvc",
"type": "Microsoft.EventGrid/eventSubscriptions"
}
Trigger registry events
Switch to the service mode to Serverless Mode
and set up a client connection to the SignalR Service. You can take Serverless Sample as a reference.
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.
git clone git@github.com:aspnet/AzureSignalR-samples.git
cd samples/Management
# Start the negotiation server
# Negotiation server is responsible for generating access token for clients
cd NegotiationServer
dotnet user-secrets set Azure:SignalR:ConnectionString "<Connection String>"
dotnet run
# Use a separate command line
# Start a client
cd SignalRClient
dotnet run
View registry events
You've now connected a client to the SignalR Service. Navigate to your Event Grid Viewer web app, and you should see a ClientConnectionConnected
event. If you terminate the client, you'll also see a ClientConnectionDisconnected
event.