Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Follow publication

Building a Chat App With SignalR, ASP.NET and jQuery

R M Shahidul Islam Shahed
Stackademic
Published in
3 min readAug 3, 2023

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.

Building a Chat App With SignalR

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";
}…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

No responses yet

Write a response