Introduction
These days, people expect apps to updates in real-time like live chats, notifications, or game scores, without refreshing the page again and again. SignalR in .NET Core helps you do just that by making real-time communication easy. In this blog, we’ll quickly show you how to get started with it in your own .NET Core project.
What is SignalR?
SignalR is a real-time messaging library developed by Microsoft that allows two-way communication between clients, like web browsers, mobile apps, or desktop applications, and the server. It offers an easy and scalable way to add real-time features to your web applications, so users can get updates and messages instantly without needing to refresh the page or make manual requests.
Usages of SignalR
Here are some common use cases where SignalR can be a great fit for adding real-time functionality to your applications.
- Chat Applications: SignalR is perfect for building mid-level chat applications where users can exchange messages instantly. It supports sending messages to all users, specific groups, or individual users in real-time. For a higher-level chatting application, XMPP or WebSocket is the best option.
- Notifications: You can use SignalR to push real-time notifications to users for events such as new messages, system alerts, or order updates without making them refresh the page.
- Live Dashboards: For use cases like monitoring systems or admin panels, SignalR enables real-time dashboards that automatically refresh as data changes, helping users make quick decisions.
- Online Exams or Quizzes: SignalR can be used in online test platforms to push time updates or instant results to participants during live sessions.
- Live Polls and Q&A: Apps that conduct live polls or Q&A during webinars or events can benefit from SignalR by showing real-time responses and updates to all viewers instantly.
- Collaboration Tools: SignalR is again helpful in mid-level collaborative apps where multiple users need to work together in real-time—like document editing, whiteboards, or project tracking tools.
How to use in an ASP.NET Core project?
To use in a .NET Core project, you have to install the SignalR NuGet package. Run the below command from NuGet Package Manager or PowerShell
# .NET CLI
dotnet add package Microsoft.AspNet.SignalR
# Nuget Package Manager
NuGet\Install-Package Microsoft.AspNet.SignalR
Now you have to create a Hub. Let's say we are building a chatbot using SignalR; hence, we are creating a chat hub. This hub will handle the server-side logic for sending messages.
// ChatHub.cs
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
Next, we have to register ChatHub in the program.cs file so that it can initialize at the time of starting.
// program.cs
var builder = WebApplication.CreateBuilder(args);
// Register services
builder.Services.AddRazorPages();
builder.Services.AddSignalR(); // Register SignalR
var app = builder.Build();
// Configure middleware
app.UseStaticFiles();
app.UseRouting();
// Map endpoints
app.MapRazorPages();
app.MapHub<ChatHub>("/chathub"); // Map Chathub with an endpoint
app.Run();
How to use in Front-end projects?
As our backend code is reday. Now let's use this in the frontend. Either you can use CDN script of SignalR or npm package of SignalR in case of Angular, React, or Next JS like below.


Comments
Join the conversation! Your thoughts help the community grow.