all Technical posts

Messaging with Azure SignalR & ASP.NET 5

This blog will outline how Azure SignalR is used for sending real- time notifications in an order processing scenario

Azure SignalR Service provides a mechanism to push data from server to client in real-time web applications.  SignalR takes advantage of several transports: the default is Web Sockets, but it falls back to other techniques like Server Sent Events or Long Polling based on the capabilities of server and client.  Chat applications are generally used as examples for SignalR, but this blog will outline how Azure SignalR is used for sending real- time notifications in an order processing scenario.

Overview

Let us look at the scenario of an order processing API with the following steps:

  1. Order processing API receives a message from a client and responds with an acknowledgment.
  2. Order API sends the message to service bus topic.
  3. A worker process receives the message from service bus and finishes order processing asynchronously.
  4. Worker Process invokes the Order API after finishing the process.
  5. Order API sends SignalR notification to the client to notify order status.

In this scenario the notification must be routed back to the correct client based on the identity of the user. We will setup the service in Azure to perform this task using the following steps.

Setup SignalR service in Azure

Azure SignalR provides the scalability to handle large scale client connections as well as remove the burden from the service to manage client connections
Create a new SignalR service in Azure and note down the connection string (Settings > Keys).

Create Order Hub

Set up a .net core API project with target framework .Net 5.0 and add reference to Microsoft.Azure.SignalR package.

It is important to understand that SignalR allows messaging in the following ways:

  • Broadcast: Sends messages to all connections.
    • // Send message to everyone
      Clients.All.SendAsync(“Notify”, “Msg…..”);
    • // Send message to everyone excluding excepted connection list
      Clients.AllExcept(excludedConnectionIds).SendAsync(“Notify”, “Msg…..”);
  • Groups: Sends messages to all connections in a named group or groups.
    • // Send message to all connections in a group
      Clients.Group(groupName).SendAsync(“Notify”, “Msg…..”);
    • // Send message to all connections in a list of groups
      Clients.Groups(groupNames).SendAsync(“Notify”, “Msg…..”);
    • // Send message to connections in a group excluding excepted connection list
      Clients.GroupExcept(groupName, excludedConnectionIds).SendAsync(“Notify”, “Msg…”);
  • Users: Sends messages to all connections associated with a specific user or users.
    • // Send message to a user
      Clients.User(userId).SendAsync(“Notify”, “Msg…..”);
    • // Send message to list of users
      Clients.Users(userIds).SendAsync(“Notify”, “Msg…..”);

In this scenario groups are used to notify order processing status to subscribing users.

Create a hub by declaring a class that inherits from DynamicHub and override the OnConnectedAsync & OnDisconnectedAsync methods in DynamicHub class. Users are added and removed from groups on connect & disconnect events respectively.

Create Response Processors

This class handles sending the notification to the client.

Here the SendOrderResponse function will be invoked on receipt of a message from worker process.

Our setup ensures the following functionality:

  • The notifications will only be sent to users belonging to the group
  • The user can start one or more sessions and create multiple orders
  • The notifications corresponding to all those orders will be sent to the same user since the user identity is used to create the group

Configure SignalR in Startup

Once the hub classes are set up, we can update the startup class.
Add the following lines in the startup class to add services required by SignalR middleware.

Now that startup is updated the API is ready to send notifications to the client.

We used Live Trace Tool, a part of Azure diagnostic settings, to monitor events.

Wrap Up

We hope you found the information provided in this blog useful. Further information and samples can be found at https://docs.microsoft.com/en-us/azure/azure-signalr/

Subscribe to our RSS feed

Thanks, we've sent the link to your inbox

Invalid email address

Submit

Your download should start shortly!

Stay in Touch - Subscribe to Our Newsletter

Keep up to date with industry trends, events and the latest customer stories

Invalid email address

Submit

Great you’re on the list!