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:

# 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:

  1. Go to the Azure portal (opens new window)
  2. Click the Create a resource button (the plus-sign in the top left corner)
  3. 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:

  1. In the Azure portal, click the Create a resource button (the plus-sign in the top left corner)
  2. 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:

  1. In the Azure portal, in the Function App, click the plus-sign next to "Functions" to start creating a new Azure Function
  2. 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
  3. In the next screen, select More templates... and click Finish and view templates
  4. Now search for "Event Grid" and select the Azure Event Grid trigger

(Event Grid trigger for the Azure Function)

  1. 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
  2. Give your Function a name and click Create
  3. 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)

  1. 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());
}
1
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:

  1. 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
  2. Open a command prompt in the AzureSignalR-samples-master directory
  3. 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
1
2
3
  1. 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
1
2
  1. 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)

  1. Close the command prompt where you are running the SignalR client in. This will disconnect the client from the hub
  2. 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!

# If you want to stay connected with me for daily software development news, then follow me on a social platform below. It means a lot!