Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Products
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. SignalR Integration in ASP.NET Core: Building a Real-Time WebSocket Chat Application

SignalR Integration in ASP.NET Core: Building a Real-Time WebSocket Chat Application

Date- May 17,2026 78
signalr aspnetcore

Overview

SignalR is a library for ASP.NET that simplifies the process of adding real-time web functionality to applications. It allows server-side code to push content to connected clients instantly, providing an efficient solution for scenarios requiring live updates. The primary problem SignalR addresses is the challenge of maintaining a persistent connection with clients to facilitate the real-time exchange of information without the need for constant polling, which can be inefficient and resource-intensive.

Real-world use cases for SignalR include chat applications, live notifications, real-time dashboards, collaborative editing tools, and gaming applications. By leveraging WebSockets and other transport protocols, SignalR ensures that data can be transmitted efficiently between the server and the client, thus enhancing user experience and engagement.

Prerequisites

  • ASP.NET Core SDK: Ensure you have the latest version installed to work with SignalR.
  • Basic C# Knowledge: Understanding C# syntax and programming fundamentals is essential.
  • Familiarity with WebSockets: Knowing how WebSockets function can help you grasp SignalR's capabilities better.
  • Visual Studio or VS Code: A suitable IDE for developing your ASP.NET Core applications.

Setting Up SignalR in ASP.NET Core

The first step in integrating SignalR into your ASP.NET Core application is setting up the necessary packages. SignalR is included as a NuGet package, which you can easily install using the .NET CLI or through Visual Studio's NuGet Package Manager.

dotnet add package Microsoft.AspNetCore.SignalR

This command adds the SignalR library to your project, allowing you to use its features in your application. Once installed, you can configure SignalR in the Startup.cs file of your ASP.NET Core project.

public class Startup
{
  public void ConfigureServices(IServiceCollection services)
  {
      services.AddSignalR();
  }

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
      if (env.IsDevelopment())
      {
          app.UseDeveloperExceptionPage();
      }
      app.UseRouting();

      app.UseEndpoints(endpoints =>
      {
          endpoints.MapHub<ChatHub>("/chat");
      });
  }
}

In this code, we define a ChatHub class that will manage the connections and messaging between clients. The ConfigureServices method adds the SignalR services to the dependency injection container, while the Configure method sets up the routing to the SignalR hub.

Creating the ChatHub Class

A hub is a class that serves as a central point for communication between clients and the server. Here’s how you can create a basic ChatHub class:

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);
  }
}

In this ChatHub class, we define a method SendMessage that takes a username and a message as parameters. It uses the Clients.All.SendAsync method to broadcast the message to all connected clients. The ReceiveMessage method will need to be implemented on the client side to handle the incoming messages.

Building the Client-Side Application

In order to send and receive messages using SignalR, we need to implement the client-side code. This can be done using JavaScript. First, we need to include the SignalR JavaScript client library in our HTML file.

<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft.signalr/3.1.0/signalr.min.js"></script>

Next, we can create a simple HTML structure for our chat application:

<!DOCTYPE html>
<html>
<head>
  <title>Chat Application</title>
</head>
<body>
  <div id="messagesList"></div>
  <input type="text" id="userInput" placeholder="Name" />
  <input type="text" id="messageInput" placeholder="Message" />
  <button id="sendButton">Send</button>
</body>
</html>

This basic HTML structure includes a list to display messages, an input for the user's name, an input for the message, and a button to send the message. The next step is to wire up the SignalR connection and handle sending and receiving messages.

Establishing the SignalR Connection

To connect to the SignalR hub, you can use the following JavaScript code:

const connection = new signalR.HubConnectionBuilder()
    .withUrl("/chat")
    .build();

connection.on("ReceiveMessage", (user, message) => {
    const msg = document.createElement("div");
    msg.textContent = \`\${user}: \${message}\`;
    document.getElementById("messagesList").appendChild(msg);
});

document.getElementById("sendButton").addEventListener("click", () => {
    const user = document.getElementById("userInput").value;
    const message = document.getElementById("messageInput").value;
    connection.invoke("SendMessage", user, message).catch(err => console.error(err.toString()));
});

connection.start().catch(err => console.error(err.toString()));

This code initializes a new SignalR connection to the /chat URL, which corresponds to the hub we set up earlier. It defines an event handler for the ReceiveMessage event, which updates the chat window when a new message is received. The click event on the send button invokes the SendMessage method on the server with the user's name and message.

Testing the Chat Application

After setting up the client and server-side code, you can run your application and test the chat functionality. Open multiple browser windows or tabs to simulate different users and try sending messages between them. You should see that messages sent from one client appear in all connected clients in real time.

Handling Disconnections

It is important to handle disconnections gracefully. SignalR provides built-in events for connection state changes, which you can use to update the UI or notify users. For instance, you can listen for the onclose event:

connection.onclose(async () => {
  console.log("Connection closed. Attempting to reconnect...");
  await start();
});

async function start() {
  try {
    await connection.start();
    console.log("Connected to SignalR server");
  } catch (err) {
    console.error(err);
    setTimeout(start, 5000);
  }
}

This code attempts to reconnect to the SignalR server if the connection is closed unexpectedly. Implementing such a feature improves the robustness of your chat application.

Edge Cases & Gotchas

When working with SignalR, there are several edge cases and potential pitfalls to be aware of:

  • Connection Limits: SignalR has a default limit on the number of concurrent connections. Be mindful of this when designing your application, especially if you expect high traffic.
  • Message Size Limits: Messages sent through SignalR have a maximum size. Large messages may be truncated, so it's advisable to implement a strategy for breaking up large data payloads.
  • Cross-Origin Requests: If your SignalR server is hosted on a different domain than your client, ensure to configure CORS properly to allow cross-origin requests.

Common Mistakes

One common mistake is not awaiting asynchronous calls in the hub methods, which can lead to unexpected behavior. Always use await with asynchronous methods in your hubs to ensure proper execution order. Additionally, failing to start the SignalR connection before invoking methods will result in errors. Ensure that connection.start() is called and awaited before sending messages.

Performance & Best Practices

Optimizing the performance of your SignalR application involves several best practices:

  • Use Connection Resiliency: Implement automatic reconnection logic to handle transient failures gracefully.
  • Minimize Payload Size: Keep messages small and concise. Use compression if necessary, especially for large data.
  • Scale Out with Backplanes: If your application runs on multiple servers, consider using a backplane like Redis to manage messages across servers efficiently.
  • Profile Your Application: Use tools like Application Insights or logging to monitor performance and identify bottlenecks in real-time.

Real-World Scenario: Building a Complete Chat Application

Now that we have covered the fundamentals, let's tie everything together by building a complete chat application. Below is the full implementation of the ASP.NET Core chat application using SignalR.

// Program.cs
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

public class Program
{
  public static void Main(string[] args)
  {
    CreateHostBuilder(args).Build().Run();
  }

  public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
      {
        webBuilder.UseStartup<Startup>();
      });
}

// Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

public class Startup
{
  public void ConfigureServices(IServiceCollection services)
  {
      services.AddSignalR();
  }

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
      if (env.IsDevelopment())
      {
          app.UseDeveloperExceptionPage();
      }
      app.UseRouting();

      app.UseEndpoints(endpoints =>
      {
          endpoints.MapHub<ChatHub>("/chat");
      });
  }
}

// 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);
  }
}

// index.html
<!DOCTYPE html>
<html>
<head>
  <title>Chat Application</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft.signalr/3.1.0/signalr.min.js"></script></head>
<body>
  <div id="messagesList"></div>
  <input type="text" id="userInput" placeholder="Name" />
  <input type="text" id="messageInput" placeholder="Message" />
  <button id="sendButton">Send</button>
  <script>
const connection = new signalR.HubConnectionBuilder()
    .withUrl("/chat")
    .build();

connection.on("ReceiveMessage", (user, message) => {
    const msg = document.createElement("div");
    msg.textContent = \`\${user}: \${message}\`;
    document.getElementById("messagesList").appendChild(msg);
});

document.getElementById("sendButton").addEventListener("click", () => {
    const user = document.getElementById("userInput").value;
    const message = document.getElementById("messageInput").value;
    connection.invoke("SendMessage", user, message).catch(err => console.error(err.toString()));
});

connection.start().catch(err => console.error(err.toString()));
</script>
</body>
</html>

This complete code implements a basic chat application using SignalR. It includes the server-side configuration for the hub and the client-side JavaScript to send and receive messages. By running this application, you can see real-time chat functionality in action.

Conclusion

  • SignalR is an essential library for implementing real-time web functionality in ASP.NET Core applications.
  • Understanding how to set up SignalR, create hubs, and manage client connections is crucial for building interactive applications.
  • Handling edge cases and optimizing performance will enhance the user experience.
  • Real-world applications like chat systems can significantly benefit from SignalR's capabilities.
  • Next steps include exploring advanced features of SignalR, such as group messaging and authentication.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Mastering Real-Time Communication with SignalR in ASP.NET Core
Mar 16, 2026
Reddit API Integration in ASP.NET Core: Handling Posts, Subreddits, and OAuth Authentication
May 24, 2026
Mastering Puppeteer Sharp for HTML to PDF Conversion in ASP.NET Core
May 20, 2026
Ably Integration in ASP.NET Core: Mastering Real-Time Pub/Sub Messaging
May 18, 2026
Previous in ASP.NET Core
Integrating IP Geolocation API in ASP.NET Core to Detect User Loc…
Next in ASP.NET Core
Integrating Twilio Video API in ASP.NET Core for Robust Video Cal…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    Implementing an End-to-End CI/CD Pipeline for ASP.NET Core… 907 views
  • 2
    Integrating Azure Cognitive Search into ASP.NET Core Appli… 629 views
  • 3
    How to get fcm server key 5,050 views
  • 4
    Responsive Slick Slider 23,598 views
  • 5
    Understanding CWE-312: Best Practices for Secure Data Stor… 298 views
  • 6
    Mastering Functions in C++: A Complete Guide with Examples 3,745 views
  • 7
    Integrating Cloudflare Turnstile in ASP.NET Core: A Privac… 142 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26293 views
  • Exception Handling Asp.Net Core 21063 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 20449 views
  • How to implement Paypal in Asp.Net Core 19809 views
  • Task Scheduler in Asp.Net core 17816 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Products
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor