Azure SignalR Service

Today, SignalR has evolved to be one of the most popular real-time connection technologies around the world. The Azure version of SignalR Service is a fully managed SignalR service that enables you to focus on building real-time web experiences without worrying about setting up, hosting, scaling, or load balancing your SignalR server.

SignalR provides an abstraction over several techniques used for building real-time web applications. Generally, WebSocket is the optimal transport that is used, but, if a WebSocket connection cannot be established, the SignalR client will fall back to either Server-Sent Events or long polling.

Sending Messages on SignalR service

Azure SignalR Service can be used in three different ways:

Scale an ASP.NET Core SignalR App – Integrate Azure SignalR Service with an ASP.NET Core SignalR application to scale out for large connections.

Build serverless real-time apps – Use Azure Functions integration with Azure SignalR Service to build serverless real-time applications in languages such as JavaScript, C#, and Java.

Free download this blog as a PDF document for offline read.

Azure Functions + SignalR Service

When you build a fully functional serverless application, sometimes you will have a need to send real-time messages. This requires a persistent connection, here you can use Web Apps for this requirement. But with Azure SignalR available now, you can use SignalR with Functions to send real-time messages from your serverless solutions. Azure Functions works well with different languages like JavaScript, C#, Java, Python etc., Therefore you can program SignalR with any of the languages that Functions support. Also, the SignalR service works well with other Azure Services.

Real-time messaging everywhere

You have clients like browser, desktop, mobiles, games etc connecting with SignalR service. Azure Functions works between the Event and SignalR service to publish the messages. For instance, you want a message to be published when an HTTP event is occurring, just set up an HTTP trigger with SignalR. Similarly, you can use timers and orchestrations when you have long-running durable functions. You can use SignalR service to broadcast the intermediate updates as well.

You can also trigger Events from Azure services, such as:

Event Grid

Event Hubs

Service Bus

Microsoft Graph

Cosmos DB change feed

Storage – blobs and queues

Logic Apps connectors such as Salesforce, Slack etc

SignalR Service bindings for Azure Functions

The SignalR Service bindings for Azure Functions allow an Azure Function app to publish messages to clients connected to SignalR Service. Clients can connect to the service using a SignalR client SDK that is available in .NET, JavaScript, and Java, with more languages coming soon. Right now there are 2 binding for Azure SignalR Service.

SignalRConnectionInfo (Input) – For a client to connect with SignalR service, you need a token. Basically, it is a connection information. This binding generates that token to connect to the client.

SignalR (Output) – This is an output binding that allows Azure Functions to send messages to SignalR service to broadcast to all the other services.

Sample SignalRConnectionInfo input binding for JavaScript

This is a JavaScript Functions, it has 2 sections, the first one is functions.js where you define the bindings. This is an input binding, therefore; the type is mentioned as “SignalRConnectionInfo”.

Connecting from a browser

Now that we have an endpoint that the client can call to retrieve the information through SignalR Services, here is how you can use it from a client. Typically, when you retrieve the information from the endpoint, you can use the standard SignalR library to create the connection.

The next step is you create one or more listeners or event triggers. In this code, we are listening for a message called “newMessage”. This JavaScript is executed when there is a “newMessage” from the server.

Sample SignalR output binding for JavaScript

Now that we have clients connected to our SignalR service, let us see how to broadcast these messages to the clients from Functions. This is an output binding therefore, the type is mentioned as “SignalR”.

Authentication and users

SignalR Service allows you to broadcast messages to all clients or only to a subset of clients, such as those belonging to a single user. Authentication and Authorization in function app allow us to use external services like Facebook, Twitter, Google+ and other services. By configuring these services, you can use them in Azure Function Apps.

Whenever a user is authenticated by an app service authentication, a list of headers are returned, from which the username is taken and sent to the signalRConnectionInfo binding. This results in a token with the authenticated user’s userId. This is used to deliver appropriate messages to appropriate users.

Related Posts

Author: Mohan Nagaraj

Mohan as a Product Marketing Executive is responsible for Product messaging and positioning, Sales Enablement, Market, customer, and competitive analysis. He combines his passion for business, technology, and writing to spread the word about Serverless360. Mohan is responsible to work with cross-functional teams to visualize and create product documentation and marketing content. He feels writing is so much fun and it is satisfying to capture the company’s soul & passion and make it live through documentation.
View all posts by Mohan Nagaraj