SignalR  

SignalR for Real-Time Web Applications

Modern web applications are no longer limited to request-and-response communication. Users now expect live notifications, instant updates, chat features, and real-time dashboards. SignalR is an ASP.NET Core library that simplifies adding real-time functionality to web applications.

SignalR enables server-side code to push content to connected clients instantly, without clients having to repeatedly poll the server. It abstracts complex real-time communication techniques such as WebSockets, Server-Sent Events, and Long Polling, automatically choosing the best available transport.

In traditional web applications, the client sends a request and waits for a response. If data changes on the server, the client must request it again. SignalR reverses this flow by allowing the server to proactively send updates to all connected clients or specific users.

Some common real-time use cases include live chat applications, stock price updates, live dashboards, notification systems, multiplayer games, and collaboration tools.

At the core of SignalR is the Hub. A Hub is a high-level pipeline that allows the client and server to call methods on each other.

Creating a SignalR Hub

using Microsoft.AspNetCore.SignalR;

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

This hub exposes a method, SendMessage, that clients can call. The server then broadcasts the message to all connected clients using Clients.All.

Registering SignalR in Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSignalR();

var app = builder.Build();

app.MapHub<NotificationHub>("/notificationHub");

app.Run();

Here, SignalR services are registered and the hub is mapped to a specific endpoint. Clients will connect to /notificationHub.

Client-Side JavaScript Integration

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

<script>
    const connection = new signalR.HubConnectionBuilder()
        .withUrl("/notificationHub")
        .build();

    connection.on("ReceiveMessage", function (user, message) {
        const msg = user + ": " + message;
        document.getElementById("messages").innerHTML += "<li>" + msg + "</li>";
    });

    connection.start();

    function sendMessage() {
        const user = document.getElementById("user").value;
        const message = document.getElementById("message").value;
        connection.invoke("SendMessage", user, message);
    }
</script>

This client code establishes a real-time connection with the SignalR hub. When the server sends a message using ReceiveMessage, the client immediately updates the UI without refreshing the page.

HTML UI Example

<input id="user" placeholder="User name" />
<input id="message" placeholder="Message" />
<button onclick="sendMessage()">Send</button>

<ul id="messages"></ul>

Once connected, multiple users can open the page and see messages appear instantly when someone sends a message.

SignalR supports multiple communication patterns. Broadcasting sends messages to all connected clients, while targeted messaging can send data to specific users, groups, or connections. Group messaging is especially useful for chat rooms, notifications by role, or department-based updates.

Another powerful feature of SignalR is automatic reconnection. If the connection drops due to network issues, SignalR can reconnect clients seamlessly, improving reliability.

SignalR also integrates well with authentication and authorization. You can restrict hub access using policies or roles, making it secure for enterprise applications.

In terms of performance, SignalR scales efficiently when combined with backplanes like Redis or Azure SignalR Service, allowing thousands of concurrent connections across multiple servers.

When to use SignalR

  • Live chat systems

  • Real-time notifications

  • Stock market or crypto price updates

  • Live dashboards and monitoring systems

  • Collaborative applications

In conclusion, SignalR dramatically simplifies real-time web development in ASP.NET Core. By abstracting low-level communication details, it allows developers to focus on business logic while delivering fast, interactive, and modern user experiences.