ASP.NET Core SignalR

Real-time web functionality for ASP.NET Core

What is ASP.NET Core SignalR?

ASP.NET Core SignalR is a library for ASP.NET Core developers that simplifies the process of adding real-time web functionality to applications. Real-time web functionality enables server-side code to push content to connected clients instantly as new data becomes available, rather than waiting for a client to request new data by polling.

Key Features

When to Use SignalR

SignalR is ideal for applications that require:

How SignalR Works

SignalR uses a technique called "abstraction over transport." This means that SignalR connects clients and servers using the best available transport method. The primary transport is WebSockets. If WebSockets are not available, SignalR falls back to other transports. The available transports are:

SignalR negotiates the best available transport automatically. Developers typically interact with SignalR through a concept called a "Hub."

Getting Started with SignalR

To get started with SignalR, you'll need to:

  1. Install the necessary NuGet packages in your ASP.NET Core project.
  2. Define a server-side Hub class that inherits from Microsoft.AspNetCore.SignalR.Hub.
  3. Configure SignalR in your application's Startup.cs or Program.cs file.
  4. Implement a client-side application (e.g., using JavaScript) to connect to the Hub and receive/send messages.

Example: Server-side Hub

Here's a simple example of a server-side Hub:


using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;

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

Example: Client-side JavaScript

And a corresponding JavaScript client:


const connection = new signalR.HubConnectionBuilder()
    .withUrl("/chat") // The URL of your SignalR Hub endpoint
    .build();

connection.on("ReceiveMessage", (user, message) => {
    const msg = message.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
    const encodedMsg = user + " says " + msg;
    const li = document.createElement("li");
    li.textContent = encodedMsg;
    document.getElementById("messagesList").appendChild(li);
});

connection.start().catch(error => {
    console.error(error.message);
});

document.getElementById("sendButton").addEventListener("click", event => {
    const user = document.getElementById("userInput").value;
    const message = document.getElementById("messageInput").value;
    connection.invoke("SendMessage", user, message).catch(err => {
        console.error(err.toString());
    });
    event.preventDefault();
});
                
Important: Make sure to configure SignalR endpoints in your application's startup code. For example, in Program.cs:

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

Explore the rest of this documentation to learn more about advanced topics such as scaling, security, and managing groups.