ASP.NET Core Real-Time with SignalR
Learn how to build real-time web applications using ASP.NET Core and SignalR.
A Simple Analogy
Imagine a walkie-talkie for your web app. Instead of sending letters (HTTP requests) and waiting for replies, SignalR lets your app talk instantly—like a live conversation. When something happens, everyone hears about it right away.
What Is SignalR?
SignalR is a library for ASP.NET Core that enables real-time communication between server and clients. It uses WebSockets (when available) or falls back to other techniques, so updates can be pushed instantly to browsers, mobile apps, or other clients.
Why Use Real-Time Communication?
- Instant updates: Users see changes as they happen (chat, notifications, dashboards)
- Collaboration: Multiple users can work together in real time (whiteboards, games)
- Live data: Stock prices, sports scores, IoT device status
- Better UX: No need to refresh or poll for changes
How SignalR Works
- Hub: Central class that manages connections and messaging
- Client: Browser, mobile app, or desktop app that connects to the hub
- Connection: Persistent link (WebSocket, SSE, or long polling)
- Methods: Server can call client methods, and clients can call server methods
Quick Setup Example
1. Add SignalR NuGet Package
dotnet add package Microsoft.AspNetCore.SignalR
2. Create a Hub
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
3. Configure in Program.cs
builder.Services.AddSignalR();
var app = builder.Build();
app.MapHub<ChatHub>("/chatHub");
4. Client-Side (JavaScript)
const connection = new signalR.HubConnectionBuilder()
.withUrl("/chatHub")
.build();
connection.on("ReceiveMessage", (user, message) => {
// Update UI with new message
});
await connection.start();
connection.invoke("SendMessage", "Alice", "Hello!");
Practical Examples
- Live chat rooms
- Real-time notifications (orders, alerts)
- Collaborative editing (documents, whiteboards)
- Live dashboards (metrics, analytics)
- Online games (multiplayer moves, scores)
Real-World Use Cases
- Customer support chat on websites
- Stock trading platforms with live price updates
- Multiplayer online games
- IoT dashboards showing device status
- Team collaboration tools (Trello, Google Docs-like apps)
Best Practices
- Use authentication to secure connections
- Scale out with Redis or Azure SignalR Service for many users
- Handle connection drops and reconnections gracefully
- Limit broadcast scope (send to groups/users, not always to all)
- Monitor and log connection events for diagnostics
- Optimize payload size for performance
Related Concepts to Explore
- WebSockets and HTTP/2
- Server-Sent Events (SSE)
- Pub/Sub messaging patterns
- Event-driven architecture
- Push notifications (mobile/web)
- Group messaging and user targeting
- Scaling real-time apps (Redis, Azure SignalR)
- Blazor Server (real-time UI updates)
- gRPC streaming
- Connection state management
Summary
SignalR makes real-time communication easy in ASP.NET Core. With just a few lines of code, you can build apps that feel alive—users see updates instantly, collaborate, and interact in ways that aren't possible with traditional request/response web apps.