Real-Time Apps Using SignalR in ASP.NET Core

Building Real-Time Applications in ASP.NET Core Using SignalR

Real-time applications update users instantly without requiring a page refresh. Examples include chat applications, live notifications, stock market updates, and real-time dashboards. In ASP.NET Core, SignalR enables developers to build these types of applications efficiently.

SignalR is a library that enables real-time communication between the server and connected clients using WebSockets. If WebSockets are not available, it automatically falls back to alternative transport mechanisms such as Server-Sent Events or Long Polling. This ensures reliability across different browsers and network environments.

What Is SignalR?

SignalR is an abstraction over multiple real-time transport protocols. It allows the server to push content to connected clients instantly.

Key capabilities include:

  • Real-time bidirectional communication

  • Automatic transport fallback

  • Built-in connection management

  • Support for authentication and authorization

  • Group-based messaging

  • Scalability using Azure SignalR Service

Installing SignalR

To use SignalR in ASP.NET Core, install the required package:

Microsoft.AspNetCore.SignalR

Creating a Hub

A Hub acts as a communication center between the server and connected clients.

Example: ChatHub

using Microsoft.AspNetCore.SignalR;

public class ChatHub : Hub
{
    public async Task SendMessage(string user, string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}

In this example:

  • ChatHub inherits from Hub.

  • SendMessage is called by clients.

  • Clients.All.SendAsync broadcasts the message to all connected clients.

Registering SignalR in Program.cs

builder.Services.AddSignalR();

app.MapHub<ChatHub>("/chathub");

This registers SignalR services and maps the hub endpoint.

Client-Side Connection (JavaScript)

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

connection.on("ReceiveMessage", (user, message) => {
    console.log(user + ": " + message);
});

connection.start();

Here:

  • A connection is created to /chathub.

  • The client listens for the ReceiveMessage event.

  • When triggered, the message is displayed.

How It Works

  1. A client sends a message to the server using SendMessage.

  2. The server broadcasts the message using Clients.All.SendAsync.

  3. All connected clients receive the message instantly.

This enables real-time communication without refreshing the page.

Advanced Features

SignalR also supports:

  • Authentication and authorization

  • User-specific messaging

  • Group-based communication

  • Scaling using Azure SignalR Service

  • Integration with microservices and cloud-native applications

Summary

SignalR simplifies the development of real-time applications in ASP.NET Core. It reduces complexity by abstracting transport protocols and providing built-in support for connection management, messaging, and scaling.

By using SignalR, developers can build fast, reliable, and scalable real-time applications with minimal setup.