21 Oct 2025
"Show me how Blazor Server uses SignalR"
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:
Hub) which acts as an intermediary between the client and server. The Hub is responsible for broadcasting messages to connected clients.How SignalR helps with real-time updates
SignalR provides a few key features that make it ideal for real-time communication:
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:
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
I continuously build, learn and experiment with innovative technology. Allow me to share what I learn, with you.