Member-only story
Building a Chat App With SignalR, ASP.NET and jQuery
Building a real-time chat application with SignalR is a great way to learn about real-time web communication. SignalR is a library in ASP.NET that simplifies the process of adding real-time capabilities to web applications. In this example, I'll provide an overview of how you can build a simple chat application using SignalR in ASP.NET Core.

Step 1: Create a New ASP.NET Core Project
- Open Visual Studio and create a new ASP.NET Core Web Application.
- Choose the "ASP.NET Core Web Application" template and select "API" as the project template.
Step 2: Install the SignalR Package
- In the Package Manager Console, run the following command to install the SignalR package:
Install-Package Microsoft.AspNetCore.SignalR
Step 3: Set up the SignalR Hub
- Create a new class called "ChatHub" that inherits from "Hub" in the project. This will act as the SignalR hub for handling real-time chat communication.
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("refreshChatBox_AddNewMessage", user);
}
}
Step 4: Configure SignalR in Program.cs
- In the "ConfigureServices" method of the "Program.cs" file, add the following code to enable SignalR:
builder.Services.AddSignalR();
- In the "Configure" method of the "Program.cs" file, add the following code to map the SignalR hub:
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/notify_RealTimeChatApp");
endpoints.MapControllers();
});
Step 5: Create the Chat UI Using Razor View
- Create a simple HTML page with JavaScript to handle the chat UI. This can be a separate HTML file or an HTML view in your project.
@model RealTimeChatApp.Models.UserProfileViewModel.UserProfileCRUDViewModel
@{
ViewData["Title"] = "User Chat";
}…