SignalR Real-Time Applications in ASP.NET Core: Complete Beginner's Guide
Modern applications increasingly rely on real-time communication. Users expect instant updates without refreshing the page, whether they are chatting with other users, receiving notifications, monitoring dashboards, or collaborating on shared documents.
ASP.NET Core SignalR provides a simple and powerful framework for adding real-time functionality to web applications. Instead of continuously polling the server for updates, SignalR enables the server to push information directly to connected clients.
In this tutorial, you'll learn how SignalR works, how to create a SignalR Hub, connect clients, send messages, and build real-time applications using ASP.NET Core.
- What is SignalR?
- Real-Time Communication
- Creating a SignalR Project
- SignalR Hubs
- Sending Messages
- JavaScript Client
- Chat Application Example
- Notifications
- Groups and Connections
- Best Practices
What Is SignalR?
SignalR is a library for ASP.NET Core that enables real-time communication between servers and clients.
It automatically selects the best available transport technology, including:
- WebSockets
- Server-Sent Events (SSE)
- Long Polling
This allows developers to focus on application logic rather than managing communication protocols.
Common SignalR Use Cases
- Live chat systems
- Real-time notifications
- Stock market dashboards
- Online gaming
- Live sports updates
- Collaborative editing tools
- Monitoring dashboards
Create a New ASP.NET Core Project
dotnet new mvc -n SignalRDemo
cd SignalRDemo
Install SignalR
SignalR is included in modern ASP.NET Core applications, but you can add the package if necessary.
dotnet add package Microsoft.AspNetCore.SignalR
Create a SignalR Hub
A Hub is the central component that manages communication between clients and the server.
using Microsoft.AspNetCore.SignalR;
public class ChatHub : Hub
{
}
Sending Messages to Connected Clients
using Microsoft.AspNetCore.SignalR;
public class ChatHub : Hub
{
public async Task SendMessage(
string user,
string message)
{
await Clients.All.SendAsync(
"ReceiveMessage",
user,
message);
}
}
The method above broadcasts messages to all connected users.
Register SignalR
Configure SignalR in Program.cs.
builder.Services.AddSignalR();
Map the Hub Endpoint
app.MapHub<ChatHub>("/chatHub");
Clients will connect using the /chatHub endpoint.
Add the SignalR JavaScript Client
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/8.0.0/signalr.min.js">
</script>
Create a Client Connection
const connection =
new signalR.HubConnectionBuilder()
.withUrl("/chatHub")
.build();
await connection.start();
Receive Messages
connection.on(
"ReceiveMessage",
function(user, message)
{
console.log(user + ": " + message);
});
Whenever the server sends a message, the client receives it instantly.
Send Messages from the Browser
await connection.invoke(
"SendMessage",
"John",
"Hello SignalR!");
Building a Simple Chat Application
A basic SignalR chat application typically includes:
- SignalR Hub
- Message input form
- Message display area
- Connected user list
Messages are delivered instantly to all connected users without refreshing the page.
Real-Time Notifications
SignalR is commonly used for notifications.
await Clients.All.SendAsync(
"NewNotification",
"Order Created Successfully");
This can be used for:
- Order updates
- System alerts
- User activity notifications
- Background job completion messages
Working with Groups
Groups allow messages to be sent to specific users.
await Groups.AddToGroupAsync(
Context.ConnectionId,
"Admins");
await Clients.Group("Admins")
.SendAsync(
"ReceiveMessage",
"Admin Notification");
Connection IDs
Every connected client receives a unique Connection ID.
string connectionId =
Context.ConnectionId;
Connection IDs make it possible to send messages to individual users.
SignalR and Dependency Injection
SignalR integrates seamlessly with ASP.NET Core's Dependency Injection system.
public class ChatHub : Hub
{
private readonly ILogger<ChatHub> _logger;
public ChatHub(
ILogger<ChatHub> logger)
{
_logger = logger;
}
}
Common Beginner Mistakes
- Not handling connection failures.
- Broadcasting unnecessary data.
- Ignoring authentication and authorization.
- Creating large Hub methods.
- Not validating incoming messages.
Best Practices
- Keep Hub methods lightweight.
- Validate user input.
- Use groups when appropriate.
- Implement authentication.
- Handle reconnection scenarios.
- Log connection events.
- Monitor application performance.
Frequently Asked Questions
Is SignalR free?
Yes. SignalR is an open-source framework maintained as part of ASP.NET Core.
Does SignalR use WebSockets?
Yes. SignalR automatically uses WebSockets when available and falls back to other transport methods when necessary.
Can SignalR be used with ASP.NET Core MVC?
Absolutely. SignalR works with MVC, Razor Pages, Blazor, and Web API projects.
Is SignalR suitable for large-scale applications?
Yes. SignalR supports scaling through technologies such as Azure SignalR Service and distributed backplanes.
Related Tutorials
- Build a REST API in ASP.NET Core
- Dependency Injection in ASP.NET Core
- Background Services in .NET
- Using HttpClient in C#
- JWT Authentication in ASP.NET Core
- ASP.NET Core Middleware
Conclusion
SignalR makes it easy to add real-time functionality to ASP.NET Core applications. Whether you're building chat systems, notifications, dashboards, or collaborative tools, SignalR provides a reliable framework for instant communication between clients and servers.
By understanding Hubs, client connections, groups, notifications, and best practices, you'll be ready to build responsive real-time applications using ASP.NET Core.