SignalR for C# (.NET): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is SignalR and why is it needed?

\n\n

SignalR is a library for ASP.NET Core that simplifies adding real-time features to web applications. It allows the server to instantly send data to connected clients (browsers, mobile applications, desktop applications) without the need for constant requests from the client.

\n\n

Why is this needed? The traditional HTTP protocol works on a "request-response" principle: the client sends a request, the server responds. To receive updates, the client must constantly poll the server (polling). This is inefficient and creates delays. SignalR solves this problem by providing a persistent two-way connection.

\n\n

Main use cases:

\n
    \n
  • Chats and messengers (instant message delivery)
  • \n
  • Monitoring panels and dashboards (updating currency rates, stock quotes)
  • \n
  • Collaborative document editing (Google Docs-like)
  • \n
  • Real-time notifications (new orders, alerts)
  • \n
  • Multiplayer games
  • \n
  • Progress of long-running operations (file uploads, data processing)
  • \n
\n\n

SignalR automatically selects the best transport protocol for the connection:

\n
    \n
  • WebSocket — the most efficient, persistent two-way connection
  • \n
  • Server-Sent Events (SSE) — one-way transmission from server to client
  • \n
  • Long Polling — fallback mechanism if WebSocket and SSE are unavailable
  • \n
\n\n

Installing SignalR

\n\n

Server-side installation (ASP.NET Core)

\n

For the server side, use the NuGet package. Run in the terminal:

\n
dotnet add package Microsoft.AspNetCore.SignalR
\n

Or via Package Manager Console in Visual Studio:

\n
Install-Package Microsoft.AspNetCore.SignalR
\n\n

Installing the client for JavaScript (browser)

\n

For the client side (browser), use npm:

\n
npm install @microsoft/signalr
\n

Or include via CDN:

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

Installing the client for .NET (C#)

\n

For a C# client (e.g., console application or WPF):

\n
dotnet add package Microsoft.AspNetCore.SignalR.Client
\n\n

Quick start: "Hello, World!" example

\n\n

Let's create a simple chat: the server receives a message and broadcasts it to all connected clients.

\n\n

Step 1: Create a Hub (server side)

\n

Create a file ChatHub.cs:

\n
using Microsoft.AspNetCore.SignalR;\n\nnamespace SignalRChat.Hubs\n{\n    public class ChatHub : Hub\n    {\n        public async Task SendMessage(string user, string message)\n        {\n            // Send the message to all connected clients\n            await Clients.All.SendAsync("ReceiveMessage", user, message);\n        }\n    }\n}\n
\n\n

Step 2: Register SignalR in Program.cs

\n
using SignalRChat.Hubs;\n\nvar builder = WebApplication.CreateBuilder(args);\n\n// Add SignalR\nbuilder.Services.AddSignalR();\n\nvar app = builder.Build();\n\napp.MapGet("/", () => "Hello World!");\n\n// Map the hub to the /chatHub path\napp.MapHub<ChatHub>("/chatHub");\n\napp.Run();\n
\n\n

Step 3: Create a client (HTML + JavaScript)

\n
<!DOCTYPE html>\n<html>\n<head>\n    <title>SignalR Chat</title>\n</head>\n<body>\n    <h2>SignalR Chat</h2>\n    <div>\n        <input type="text" id="userInput" placeholder="Your name" />\n        <input type="text" id="messageInput" placeholder="Message" />\n        <button onclick="sendMessage()">Send</button>\n    </div>\n                    

Recommendations