Skip to main content Explore View all products (200+) Microsoft Foundry Azure Copilot GitHub Copilot Azure Kubernetes Service (AKS) Azure Cosmos DB Azure Database for PostgreSQL Azure Arc Microsoft Fabric Linux virtual machines in Azure Foundry Models Foundry Agent Service Foundry IQ Foundry Tools Foundry Control Plane Observability in Foundry Control Plane Azure OpenAI in Foundry Models Azure Speech in Foundry Tools Azure Machine Learning View all databases Azure Cosmos DB Azure DocumentDB Azure SQL Azure Database for PostgreSQL Azure Managed Redis Microsoft Fabric Azure Databricks Linux virtual machines in Azure Windows Server on Azure Azure Functions Azure Virtual Machine Scale Sets Azure API Management Azure Container Apps Azure Kubernetes Service (AKS) Azure Kubernetes Fleet Manager Azure Container Registry Azure Red Hat OpenShift Azure Container Instances Azure Container Storage Azure Arc Azure Local Microsoft Defender for Cloud Azure Monitor Microsoft Sentinel Azure Migrate View all solutions (40+) Cloud solutions for small and medium businesses Cloud migration and modernization center Data analytics for AI Azure Databases AI apps and agents Microsoft Marketplace Microsoft Sovereign Cloud AI apps and agents Responsible AI with Azure AI Infrastructure Data analytics for AI Machine learning operations (MLOps) Low-code application development on Azure Integration Services Serverless computing DevOps Migration and modernization center .NET apps migration Databases on Azure Linux on Azure Oracle on Azure SAP on the Microsoft Cloud Adaptive cloud High-performance computing (HPC) Infrastructure as a service (IaaS) Resiliency Azure Essentials Frontier Accelerate for Azure FinOps on Azure Microsoft Marketplace Azure pricing overview Create an Azure account Free Azure services Flexible purchase options Pricing calculator FinOps on Azure Maximize ROI from AI Azure savings plans Azure reservations Azure Hybrid Benefit Virtual Machines Azure SQL Microsoft Foundry Microsoft Fabric Azure Kubernetes Service (AKS) Microsoft Defender for Cloud View more Software Development Companies Microsoft Marketplace Find a partner Resources for Azure partners Get started with Azure Customer stories Analyst reports, white papers, and e-books Videos Learn more about cloud computing Documentation Explore Azure portal Developer resources Quickstart templates Resources for startups Developer community Students Azure for partners Blog Events and Webinars Learn Support Contact Sales Get started with Azure Sign in

header

There were lots of announcements at the Microsoft Build 2018 conference, but one that caught my eye was the preview release of Azure SignalR, a Platform-as-a-Service (PaaS) offering that lets you implement real-time messages and notifications quite easily, without worrying about instances or hosts.

So it made me wonder, could I build something using my favorite globally-distributed and serverless database, Azure Cosmos DB, and Azure’s serverless compute offering, Azure Functions? It turns out others were interested in this topic too.

Real-time, really?

For those of you that do not know, SignalR is a library that’s been around since 2013 for the ASP.NET Framework, recently rewritten for ASP.NET Core under the name of SignalR Core, that allows you to easily create real-time applications and push content to clients through the Websocket protocol, gracefully falling back to other alternatives depending on the client. It works great for games, dashboards/monitoring apps, collaborative apps, mapping/tracking apps, or any app requiring notifications.

By leveraging the Websocket protocol, content can be pushed to clients without the overhead of opening multiple HTTP connections and over a single two-way TCP channel that is maintained for the entire session.

Going serverless!

One requirement of SignalR was that you, the developer, had to have a host to manage and maintain connections with clients, usually deployed as a Web instance. In Azure, you can do it in any of the PaaS offerings like App Service and Cloud Services. While it wasn’t a complex task, it still meant you had to maintain and ensure the service could handle a number of connections and the load based on your design and user load definitions.

With Azure SignalR, the host is provided for you. You only need to connect the clients and servers to the exposed service endpoint. You can even scale the service based on your needs transparently.

If you add a serverless database plus Azure Functions as a serverless platform to run all your code, then the result would be… Serverless, Serverless everywhere!

Streamlining notifications

The architecture would need:

  • A client that can save data and receive real-time notifications. For simplicity, a web client was the faster option and to showcase we’ve picked a common chat application.
  • An API that can receive data from the clients and save them to the database.
  • Some mechanism that can act upon new data and notify all clients in real-time.

Since I was using Azure Functions (in Consumption Plan) as my compute layer, I distributed all functionality in four functions:

  • FileServer which acts as a serverless file server for static files that will let a web client browse and obtain the files in the www folder, as if there was a web host continually running (but shhh, there isn’t!). The web client is using Azure SignalR’s npm package for connectivity and transport protocol resolution. Alternatively, static assets could be served through Azure CDN too.
  • SaveChat, which will receive chat messages from the connected web clients and save it to Cosmos DB using Output bindings.
  • SignalRConfiguration, which will send the required information to the web client to initialize SignalR’s Websocket connection.
  • FeedToSignalR, which will trigger a CosmosDB Trigger, based on new data in Azure Cosmos DB, and broadcast it through Azure SignalR to all connected clients.

In order to support custom routes, in particular for static web host, I implemented Azure Functions Proxies through a proxies.json file. So, when the user browses the base URL, it is instead calling one of the HTTP triggered functions.

The complete flow is as follows:

  • When the web client loads the static resources (e.g., browses the base URL), it pulls the SignalR configuration from SignalRConfiguration.
  • It will then negotiate with Azure SignalR the best transport protocol that the browser supports and connect.

signalr

  • When the user writes a message, it will save it to Azure Cosmos DB via an Ajax call to SaveChat.

chat

  • Each chat line is stored as a document in Azure Cosmos DB.

db

  • The FeedToSignalR will trigger and broadcast it to all Azure SignalR connected clients.

invocation

The final result is a complete serverless flow that consumes resources only when needed, and doesn’t need to maintain any host or extra service layer.

While this is just a chat app, it can easily be implemented in other flows like IoT, dashboards, system-wide broadcasts, and a big etcetera, dream big!

Next steps

The code is public in Github and it even includes a nice button that will deploy the complete architecture for you in your Azure subscription in one click!

deploybutton

Clone, fork, and use it as a base for your new or current projects. Just remember that Azure SignalR is still in preview, the libraries or service APIs might change in the future. You can try Azure Cosmos DB for free today, no sign up or credit card required. Stay up-to-date on the latest Azure Cosmos DB news and features by following us on Twitter #CosmosDB, @AzureCosmosDB.

WE ARE MICROSOFT

Explore Microsoft Foundry

The future of AI starts here. Envision your next great AI app with the latest technologies. Get started with Azure.