liveBook · Manning

SignalR connection

As explained in Building Distributed Applications that Work

A client-side communication object that configures and maintains a connection to a SignalR hub.

A SignalR connection is the client-side object that connects a UI to a hub. Build it with a hub URL, register handlers for server events, and start it when the UI is ready. In an order-tracking page, the connection receives location updates that move a marker on a map. section 4.4.2

For a Blazor page connecting to a separate Aspire-hosted API service:

  • Register HTTP client services, inject IHttpMessageHandlerFactory, and pass it to the connection builder. A WithUrl extension assigns a handler from that factory to the connection options, enabling service discovery for a named-service URL such as https://apiservice/locationHub. section 4.4.2
  • Build the connection during component initialization. After the first render, initialize the map, register the ReceiveLocationUpdate handler, then call StartAsync. This order lets the handler update the rendered map when coordinates arrive. section 4.4.2
Listing 4.2 Extension class for SignalR service discovery
using Microsoft.AspNetCore.SignalR.Client;

namespace OnlineShop.Web.Extensions;

public static class HubConnectionExtensions
{
    public static IHubConnectionBuilder WithUrl(
        this IHubConnectionBuilder builder,
        string url,
        IHttpMessageHandlerFactory clientFactory)
    {
        return builder.WithUrl(
            url, options =>
        {
            options.HttpMessageHandlerFactory =
                _ => clientFactory.CreateHandler();
        });
    }
}

The WithUrl extension supplies the connection’s HTTP message handler through IHttpMessageHandlerFactory, so the Aspire service name can be resolved. section 4.4.2

Browser chat connection

A JavaScript chat client builds its hub URL from the application base URL, enables automatic reconnect and logging, and reports reconnecting, connected, or disconnected status in the UI. Once started, the same connection opens a StreamAnswer stream; the page appends incoming chunks and can dispose of the subscription when the user presses Stop. section 5.3.1

How Building Distributed Applications that Work covers it

How deep it goes
Shows connection construction, handler registration, startup timing, and browser reconnection callbacks without examining transport internals.
Examples
Uses a Blazor order-tracking map receiving coordinates and a JavaScript chat page displaying streamed characters.
What it assumes
Assumes working Blazor or JavaScript UI code while showing SignalR client construction and Aspire handler registration explicitly.
What you can do after
Build it from scratch with SignalR client builders, then register update handlers and start the connection at the appropriate UI lifecycle point.
How it connects
Ties SignalR hubs to Aspire service discovery, Blazor component lifecycle, JavaScript interop, and streaming.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Distributed Applications that Work. The section links point to the parts of the book it draws on.