How Blazor Server uses SignalR

How Blazor Server uses SignalR

21 Oct 2025

Prompt

"Show me how Blazor Server uses SignalR"

Answer

Blazor Server is a component-based framework for building web applications that run on the server. One of its key features is the ability to use SignalR, a real-time communication library provided by Microsoft, for bidirectional communication between the client and server.

Here's a high-level overview of how Blazor Server uses SignalR:

SignalR in Blazor Server

When you create a new Blazor Server project, it comes with a basic setup that includes SignalR. Under the hood, Blazor Server uses SignalR to establish a persistent connection between the client (the browser) and the server.

Here's what happens behind the scenes:

  1. Client-side: When a user navigates to your Blazor Server app, their browser establishes a WebSocket connection to the server.
  2. Server-side: On the server, a Hub is created (by default, it's called Hub) which acts as an intermediary between the client and server. The Hub is responsible for broadcasting messages to connected clients.
  3. Connection establishment: When the client connects to the server via WebSocket, it establishes a connection to the Hub. The Hub then assigns a unique connection ID to the client.
  4. Message broadcasting: Whenever your Blazor Server app wants to send data to connected clients (e.g., updating UI elements), it uses the Hub to broadcast a message to all connected clients.

How SignalR helps with real-time updates

SignalR provides a few key features that make it ideal for real-time communication:

  1. Persistent connections: Once established, the WebSocket connection remains open until the client disconnects or the server closes the connection.
  2. Server-side push: The Hub can send messages to connected clients without requiring any additional requests from them.

In Blazor Server, SignalR is used to update the UI in real-time, by broadcasting changes to all connected clients through the Hub. For example:

  • When a user updates their profile information, the server sends an update message to all connected clients, which then update their respective UI elements.
  • If multiple users are viewing the same data (e.g., a shared dashboard), any changes made by one user will be reflected in real-time across all connected clients.

Example code

Here's some example Blazor Server code that demonstrates how SignalR is used:

// Hub.cs
public class MyHub : Hub
{
    public void SendMessage(string message)
    {
        Clients.All.SendAsync("ReceiveMessage", message);
    }
}

// Page.razor
@page
@using Microsoft.AspNetCore.SignalR

<h1>Real-time updates</h1>

<input @bind="message" />
<button @onclick="UpdateMessage">Send Message</button>
<div>@message (updated in real-time)</div>

@code {
    private string message = "";

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await HubConnection.Start();
        }
    }

    void UpdateMessage()
    {
        MyHub.Connection.SendMessage(message);
    }
}

In this example, the MyHub class represents a SignalR Hub that broadcasts messages to all connected clients. The Page.razor file uses the Hub to send updates to connected clients when the user clicks the "Send Message" button.

That's a basic overview of how Blazor Server uses SignalR!


Every now and then I have to prioritize work, life or other matters but we'll soon continue with the ML Basics on YouTube

Kind regards, Auguste @ kiss-code


Join the community

I continuously build, learn and experiment with innovative technology. Allow me to share what I learn, with you.

Newsletter

Allow me to share what I learn, with you.

Share

Support

An error has occurred. 🗙