Introduction

Modern web applications often need to update information instantly without requiring users to refresh the page. Examples include chat systems, live dashboards, stock prices, order tracking, notifications, and collaborative editing.

ASP.NET Core SignalR makes this possible by providing real-time communication between the server and connected clients.

In this guide, you'll learn how SignalR works, how to create hubs, connect Blazor clients, and implement real-time features.


What is SignalR?

SignalR is a real-time communication library for ASP.NET Core.

It enables the server to send updates to connected clients immediately.

Instead of repeatedly asking the server for new information (polling), clients receive updates as soon as they happen.


How SignalR Works

Blazor Client
        │
        â–ŧ
 SignalR Hub
        │
        â–ŧ
ASP.NET Core Server
        │
        â–ŧ
Database / Services

When Should You Use SignalR?

SignalR is ideal for:


Installing SignalR

Install the required package:

dotnet add package Microsoft.AspNetCore.SignalR

Creating a SignalR Hub

Example:

using Microsoft.AspNetCore.SignalR;

public class ChatHub : Hub
{
    public async Task SendMessage(string user, string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}

The hub acts as the communication endpoint between the server and clients.


Registering SignalR

In Program.cs:

builder.Services.AddSignalR();

app.MapHub<ChatHub>("/chatHub");

Connecting from a Blazor Client

Install:

dotnet add package Microsoft.AspNetCore.SignalR.Client

Create a connection:

hubConnection = new HubConnectionBuilder()
    .WithUrl(Navigation.ToAbsoluteUri("/chatHub"))
    .WithAutomaticReconnect()
    .Build();

Start it:

await hubConnection.StartAsync();

Receiving Messages

hubConnection.On<string, string>(
    "ReceiveMessage",
    (user, message) =>
    {
        messages.Add($"{user}: {message}");
        InvokeAsync(StateHasChanged);
    });

Sending Messages

await hubConnection.InvokeAsync(
    "SendMessage",
    user,
    message);

Connection Lifecycle

Handle connection events:

hubConnection.Reconnecting += error =>
{
    // Update UI
    return Task.CompletedTask;
};

hubConnection.Reconnected += id =>
{
    return Task.CompletedTask;
};

hubConnection.Closed += error =>
{
    return Task.CompletedTask;
};

Authentication with SignalR

SignalR supports authenticated users.

Example:

[Authorize]
public class ChatHub : Hub
{
}

This allows you to send messages only to authenticated users.


Broadcasting to Specific Users

Send to everyone:

await Clients.All.SendAsync(...);

Send to one user:

await Clients.User(userId)
.SendAsync(...);

Send to a group:

await Clients.Group("Sales")
.SendAsync(...);

Common Errors

Hub Connection Failed

Usually caused by:


WebSocket Connection Closed

Possible reasons:

SignalR automatically falls back to other transports if WebSockets are unavailable.


Hub Method Not Found

Check:


Best Practices


Real-World Use Cases

SignalR is commonly used for:


Conclusion

SignalR enables developers to build responsive, real-time applications with minimal effort. Combined with Blazor, it provides an excellent framework for creating dashboards, chat systems, notifications, and other interactive features that update instantly.


Frequently Asked Questions

What is SignalR used for?

SignalR enables real-time communication between ASP.NET Core servers and connected clients.

Does SignalR use WebSockets?

Yes. SignalR prefers WebSockets and automatically falls back to other transports if necessary.

Can Blazor use SignalR?

Yes. Both Blazor Server and Blazor WebAssembly can communicate with SignalR hubs to receive and send real-time updates.

🤖 AlgoLassi Assistant Have a question about this tutorial?

Ask AlgoLassi and get an answer plus the tutorials worth studying next.

Ask a question

đŸ’Ŧ Comments

Sign in with Google to publish immediately, or comment anonymously and wait for approval.

Comments will appear here when available.