Introduction
Modern applications increasingly rely on real-time communication to deliver instant updates and interactive experiences. Whether it's a chat application, live dashboard, stock market tracker, multiplayer game, or notification system, users expect data to update immediately without refreshing the page.
Traditionally, web applications relied on polling techniques where clients repeatedly asked the server for updates. This approach increases server load and introduces delays.
SignalR solves this problem by enabling real-time communication between clients and servers.
In this article, you'll learn what SignalR is, how it works, and how to build real-time applications using SignalR and ASP.NET Core.
What Is SignalR?
SignalR is a real-time communication library for ASP.NET Core that allows servers to push updates to connected clients instantly.
Instead of clients constantly requesting updates, the server can send information whenever data changes.
SignalR supports:
Real-time messaging
Live notifications
Collaborative applications
Dashboard updates
Multiplayer experiences
It simplifies the process of adding real-time functionality to web applications.
Why Use SignalR?
SignalR offers several advantages over traditional communication methods.
Instant Updates
Clients receive updates immediately when data changes.
Reduced Server Requests
No need for continuous polling.
Better User Experience
Applications feel more responsive and interactive.
Cross-Platform Support
SignalR works with:
ASP.NET Core
JavaScript
Blazor
Mobile applications
Automatic Connection Management
SignalR handles connection establishment and reconnection automatically.
How SignalR Works
SignalR creates a persistent connection between clients and the server.
Typical workflow:
Client
↕
SignalR Hub
↕
Server
When the server sends data, connected clients receive it instantly.
This communication is bidirectional, meaning both the server and clients can exchange messages.
SignalR Transport Methods
SignalR automatically chooses the best available communication method.
Supported transports include:
WebSockets
Preferred option for maximum performance.
Client ↔ Server
Server-Sent Events (SSE)
Used when WebSockets are unavailable.
Long Polling
Fallback option for older environments.
SignalR automatically selects the most appropriate transport.
Common Real-Time Use Cases
SignalR is commonly used for:
Chat applications
Live dashboards
Notifications
Online gaming
Collaborative editing
Financial applications
Monitoring systems
Any application requiring immediate updates can benefit from SignalR.
Creating an ASP.NET Core Project
Create a new ASP.NET Core project.
dotnet new webapp -n SignalRDemo
Navigate to the project folder.
cd SignalRDemo
Run the application.
dotnet run
The project is now ready for SignalR integration.
Adding SignalR
Install the SignalR package.
dotnet add package Microsoft.AspNetCore.SignalR
This package provides all required SignalR functionality.
Creating a SignalR Hub
A Hub acts as the communication center between clients and the server.
Create a new file named:
ChatHub.cs
Add the following code.
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 receives messages and broadcasts them to all connected clients.
Registering SignalR Services
Open Program.cs.
Add SignalR services.
builder.Services.AddSignalR();
Map the hub endpoint.
app.MapHub<ChatHub>("/chatHub");
SignalR is now configured and ready to accept connections.
Creating a Client Connection
Add the SignalR JavaScript library.
<script src="
https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/8.0.0/signalr.min.js">
</script>
Create a connection.
const connection =
new signalR.HubConnectionBuilder()
.withUrl("/chatHub")
.build();
This establishes communication with the server.
Starting the Connection
Start the connection after building it.
connection.start()
.then(() => {
console.log(
"Connected"
);
});
Join the conversation! Your thoughts help the community grow.