Featured image of post Real-time Updates with SignalR and Azure Functions

Real-time Updates with SignalR and Azure Functions

Stop Polling. Seriously, stop it.

If I have to review one more Pull Request where the frontend polls an API every 2 seconds to check if a job is done, I might lose it. We live in the future. We have WebSockets. We have technology!

For our logistics platform, drivers and dispatchers need to see updates instantly. If a route deviation occurs, the dashboard needs to flash red immediately, not 2 seconds later.

Serverless SignalR

The challenge with traditional SignalR is scaling. You need to manage connections, handle sticky sessions, and provision servers just for holding open WebSockets. I don’t want to manage servers. I want to write code.

Enter Azure SignalR Service in Serverless mode.

We paired this with Azure Functions to create a completely serverless notification hub. The backend API doesn’t even know about WebSockets; it just drops a message onto a Service Bus queue. An Azure Function picks up that message and “pushes” it to SignalR.

The Code

Here is how ridiculously simple the Azure Function is. It uses the SignalR output binding.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class NotificationFunctions
{
    [Function("BroadcastToDashboard")]
    [SignalROutput(HubName = "dashboard", ConnectionStringSetting = "AzureSignalRConnectionString")]
    public SignalRMessageAction Run(
        // Trigger on a Service Bus Message
        [ServiceBusTrigger("notifications", "dashboard-sub", Connection = "ServiceBusConnection")] 
        string messageBody,
        FunctionContext context)
    {
        var logger = context.GetLogger("NotificationFunctions");
        logger.LogInformation($"Broadcasting: {messageBody}");

        // The binding handles the rest!
        return new SignalRMessageAction("newNotification")
        {
            Arguments = new[] { messageBody }
        };
    }
}

Why It’s Great

  1. Decoupled: Our main API doesn’t need to maintain thousands of open connections.
  2. Scalable: Azure SignalR Service handles the heavy lifting.
  3. Cost-Effective: In Serverless mode, you pay per update sent, not for idle server time.

It’s a cleaner, faster, and more modern way to keep your users in the loop. Stop polling!

All rights reserved
Built with Hugo
Theme Stack designed by Jimmy