TIP
🦄 Follow me on Twitter for daily software updates and a chance to get FREE Azure sticker set shipped anywhere!
🦄 Follow me on Twitter (opens new window) for daily software updates.
💡 Learn more : Azure Event Grid overview (opens new window).
📺 Watch the video : How to use Azure SignalR Service and Event Grid walkthrough (opens new window)
# Azure SignalR Service and Event Grid Walkthrough
# Serverless SignalR
You can use Azure SignalR Service (opens new window) to send messages to connected clients and create real-time applications. You can also use Azure SignalR Service in a serverless mode (opens new window), which means that you don't have a SignalR server that always runs, but that you can directly send messages to clients through the REST API (opens new window) or the SignalR Management SDK (opens new window). Running SignalR in serverless mode is great, because you don't have to worry about maintaining the SignalR server and because it scales automatically. But when you run in serverless mode, you don't have access to client events, like the OnConnected() event (opens new window), which tells you that a client has just connected to the SignalR Hub.
There is a way to get client events like OnConnected() when you run in serverless mode. SignalR Service can send client events to an Azure Event Grid (opens new window), so that you can subscribe to those events and respond to them.
In this post, we'll use an Azure SignalR Service in serverless mode, together with an Azure Function (opens new window), and subscribe to client events with an Azure Event Grid.
# Prerequisites
If you want to follow along, you'll need the following:
- An Azure subscription (If you don't have an Azure subscription, create a free account (opens new window) before you begin)
- The latest version of the .NET Core runtime (opens new window) installed on your local machine
# Use Azure SignalR Service with Azure Event Grid
Let's get started! First, we need to create an Azure SignalR Service that runs in serverless mode:
- Go to the Azure portal (opens new window)
- Click the Create a resource button (the plus-sign in the top left corner)
- Search for SignalR and click on the result to start creating one a. This brings you to the create blade of Azure SignalR Service b. Select a Resource Group (or create a new one) c. Fill in a name for the service in the Resource Name field d. Select a Pricing tier. The Free tier is fine for this example e. Make sure to select Serverless for the ServiceMode f. Click Review + create and Create after that, to create the SignalR Service
(Create Azure SignalR Service in the Azure portal)
Next, we need to create an Azure Function that will receive the OnConnected() and OnDisConnected() client events from Azure Event Grid. To do that, we first need to create an Azure Function App:
- In the Azure portal, click the Create a resource button (the plus-sign in the top left corner)
- Search for Function App and click on the result to start creating one a. This brings you to the create blade of Azure Function b. Type in a name for the Function App c. Select a Resource Group (or create a new one) d. Leave the rest as it is and click Create to create the Function App
(Create Azure Function in the Azure portal)
When the Function App is created, we need to create an Azure Function in it and configure it to be triggered by Azure Event Grid:
- In the Azure portal, in the Function App, click the plus-sign next to "Functions" to start creating a new Azure Function
- Select In-portal for the Development Environment and click Continue. This will allow us to write the code for the Azure Function in the Azure portal
- In the next screen, select More templates... and click Finish and view templates
- Now search for "Event Grid" and select the Azure Event Grid trigger
(Event Grid trigger for the Azure Function)
- It will ask to install the Event Grid extension into the Azure Function. Click Install to do this and click Continue when it is finished
- Give your Function a name and click Create
- Now that the Azure Function is created, we need to configure the Event Grid subscription for it. Click Add Event Grid subscription to fill in the subscription details
(Add Event Grid subscription to Azure Function)
a. Fill in a Name for the subscription b. For Topic Types, select Azure SignalR Service c. Select the Subscription that contains the Azure SignalR Service d. Select the Resource Group that contains the Azure SignalR Service e. And finally, select the SignalR Service f. Click Create to create the subscription
(Configure Event Grid subscription in the Azure portal)
- The Function will now be triggered whenever the client events OnConnected() or OnDisConnected() are sent by the Event Grid. To make sure that we can see which event triggered the Function, we'll add an additional line of code to the Function, to make the code of the Function look like this:
#r "Microsoft.Azure.EventGrid"
using Microsoft.Azure.EventGrid.Models;
public static void Run(EventGridEvent eventGridEvent, ILogger log)
{
log.LogInformation(eventGridEvent.EventType);
log.LogInformation(eventGridEvent.Data.ToString());
}
2
3
4
5
6
7
8
Let's test it! If we connect or disconnect a client to the hub of the Azure SignalR Service, the Azure Function will be triggered by the Event Grid. We can do that by using a sample application:
- Go to https://github.com/aspnet/AzureSignalR-samples (opens new window) and clone or download it, so that you have the code on your computer
- Open a command prompt in the AzureSignalR-samples-master directory
- Start the sample negotiation server application with the code below. You can find the SignalR connection string in the Keys menu of the Azure SignalR Service in the Azure portal
cd samples\Management\NegotiationServer
set Azure__SignalR__ConnectionString=<your_signalr_connection_string>
dotnet run
2
3
- Now open another command prompt in the AzureSignalR-samples-master directory and run the sample SignalR client with the following code. This will connect a client to the SignalR hub
cd samples\Management\SignalRClient
dotnet run
2
- Go to the Azure Function in the Azure portal and look at the logs. You'll see an event of the type Microsoft.SignalRService.ClientConnectionConnected, which means that a client just connected to the hub
(Result of the client connecting to the SignalR hub)
- Close the command prompt where you are running the SignalR client in. This will disconnect the client from the hub
- Check the logs of the Azure Function again. You'll see another event that tells you that the client disconnected
# Conclusion
Using Azure SignalR Service (opens new window) in a serverless mode provides a lot of advantages but cuts out some functionality. You can add this functionality back by using Azure Event Grid to publish SignalR client events (opens new window). Go and check it out!