A SignalR hub is a high-level API for calls between a server and connected clients. It lets a client invoke a server method and lets the server invoke a named client handler, without implementing WebSocket communication directly. In a delivery-tracking service, the hub carries location updates to customers following an order. section 4.1 section 4.3
Listing 4.1 SignalR hub for order location tracking
using Microsoft.AspNetCore.SignalR;
namespace OnlineShop.ApiService;
public class LocationHub : Hub
{
public async Task UpdateLocation(
double latitude,
double longitude)
{
await Clients.All.SendAsync(
"ReceiveLocationUpdate",
latitude,
longitude);
}
}
The hub inherits from `Hub`. Connected clients can call `UpdateLocation` with latitude and longitude; `Clients.All.SendAsync` then invokes their `ReceiveLocationUpdate` handler with those coordinates. Broadcasting to every client keeps this example simple—SignalR can also address specific clients. section 4.3.1
To expose the hub, register SignalR services with `builder.Services.AddSignalR()` and map the class to an endpoint with `app.MapHub<LocationHub>("/locationHub")` before `app.Run()`. The path is a chosen URL relative to the service address. SignalR is included in the ASP.NET Core SDK, so this backend needs no additional SignalR NuGet dependency. section 4.3.2 section 4.3.1
A hub abstracts the connection transport: SignalR uses WebSockets by default and can fall back to server-sent events or long polling. For deployment across multiple servers, backplanes such as Redis, Azure SignalR Service, or SQL Server can carry updates to clients connected to different servers. section 4.1
