C# / .NET Compendium PLEN
.NET / C# / Web API, Blazor

SignalR in Blazor Server - how it works under the hood

The fundamental difference from the traditional web

In the classic HTTP request-response model, every interaction (a click, typing text) requires a separate request and response - the connection ends once the response is delivered. Blazor Server works differently - it maintains a persistent, two-way connection (a WebSocket, with a Long Polling fallback) through SignalR for the whole duration of the user's session.

The flow step by step

1. The browser loads the page → a small HTML/JS file (blazor.server.js), with practically no logic
2. The JS establishes a SignalR (WebSocket) connection to the server
3. The server creates a "circuit" object - the state of your Blazor session (component instances, their fields, etc.)
4. The server renders the component into a diff tree and sends the rendering instructions over the WebSocket
5. The browser applies these instructions to the real DOM (JS interop, but minimal)

What happens on a user interaction (e.g. a click)

<button @onclick="ShipOrder">Ship the order</button>

@code {
    private async Task ShipOrder()
    {
        await OrderService.ShipOrderAsync(Order.Id);
        Order.Status = "Shipped";
    }
}
1. The user clicks the button in the browser
2. The JS (blazor.server.js) does NOT run any logic locally -
   it sends a message over the WebSocket to the server: "onclick was clicked on this element"
3. The server receives the event, finds the appropriate component instance (in the "circuit"),
   and calls the ShipOrder() method - ALL the C# logic runs on the server
4. await OrderService.ShipOrderAsync(...) - a normal async call, as in any backend
5. Order.Status = "Shipped" - a state change in memory ON THE SERVER
6. Blazor automatically detects the state change, computes the difference (diff) in the virtual UI tree
7. The server sends ONLY the difference over the WebSocket (e.g. "change the text of this element to X"),
   NOT the whole page again
8. The JS in the browser applies this small change to the real DOM

The key takeaway: no C# code ever reaches the browser in Blazor Server - the browser is in practice a "thin terminal" displaying the UI and passing on events, while all the logic and all the state live on the server for the whole duration of the session.

The "circuit" - the cost of keeping state on the server

This is a common question about the drawbacks of this approach - every connected user is a separate "circuit" held in server memory, with the state of all their components (analogous to the Scoped lifetime we discussed with DI - a circuit is practically the equivalent of a "scope" per user, lasting the entire session, not just one request).

Practical consequences: - Losing the connection (e.g. WiFi dropping) = after some time the server removes the circuit and all the state is lost - the user has to refresh the page - Horizontal scaling requires so-called sticky sessions (the load balancer has to always route the same user to the same server, because the circuit lives only on one instance) - this is a real architectural limitation - A large number of concurrent users = a lot of memory taken up by circuit states on the server

This is exactly the opposite of what we discussed with DbContext/Scoped in ASP.NET Core Web API, where state lives only for the duration of one request and is released immediately.

Buy me a coffee