ASP.NET SignalR Documentation

Real-time web functionality for .NET

Introduction to SignalR

ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of adding real-time web functionality to applications. Real-time web functionality is the ability to have your server push content to connected clients instantly as it becomes available.

SignalR supports two server-side models:

SignalR also handles automatically determining the best available transport mechanism for the client and server to use. This includes:

Core Concepts

Understanding the following core concepts is crucial for effectively using SignalR:

Transport Mechanisms

SignalR's ability to abstract away the transport layer is one of its key strengths. It dynamically selects the most efficient and compatible transport:

Hub Architecture

The Hub model simplifies real-time communication by providing a strongly-typed connection between clients and the server. A Hub class is defined on the server, and corresponding proxy classes are generated on the client.

Server-Side Hub Definition:


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

Client-Side Invocation (JavaScript Example):


            "use strict";

            var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();

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

            connection.start().then(function () {
                console.log("SignalR Connected");
            }).catch(function (err) {
                return console.error(err.toString());
            });

            document.getElementById("sendButton").addEventListener("click", function (event) {
                var user = document.getElementById("userInput").value;
                var message = document.getElementById("messageInput").value;
                connection.invoke("SendMessage", user, message).catch(function (err) {
                    return console.error(err.toString());
                });
                event.preventDefault();
            });
            

Client-Side Programming

SignalR provides client libraries for various platforms, including JavaScript, .NET (for UWP, WPF, Xamarin, etc.), and others. The JavaScript client is most common for web applications.

Key aspects of client-side programming include:

Server-Side Programming

On the server, you configure SignalR and define your Hub classes. This involves:

Note on ASP.NET Core SignalR

While this documentation focuses on the general concepts, ASP.NET Core SignalR is the modern and recommended implementation for building real-time web applications with .NET. It offers improved performance and a streamlined development experience.

Getting Started

To start using SignalR in your ASP.NET application:

  1. Install the SignalR NuGet package. For ASP.NET Core, this is typically Microsoft.AspNetCore.SignalR.
  2. Configure SignalR services in your Startup.cs file (or equivalent in newer .NET versions).
  3. Map the SignalR endpoint using app.MapHub<YourHubClass>("/yourHubPath");.
  4. Include the SignalR JavaScript library in your client-side HTML.
  5. Create your Hub class and implement the desired server-side logic.
  6. Write client-side JavaScript to connect to the Hub and handle real-time events.

Development Tip

Use browser developer tools to inspect network requests and understand how SignalR is communicating with the server. This is invaluable for debugging.