WebSocket library C# (.NET): SignalR and alternative tools for real-time
In modern web development, the need for two-way real-time communication has become standard. Chat applications, online games, trading platforms, server monitoring — all of these require instant data transfer. For the C# (.NET) language, there are several powerful libraries implementing the WebSocket protocol. In this article, we will thoroughly examine SignalR — Microsoft's flagship library, and also consider alternatives: WebSocketSharp and Fleck. You will learn how to choose the right tool and how to quickly get started.
What is WebSocket and why is it needed in C#
WebSocket is a protocol that provides full-duplex communication between a client and server over a single long-lived TCP connection. Unlike HTTP, where the client sends a request and waits for a response, WebSocket allows the server to initiate data sending at any time. For the .NET platform, this is especially important, as C# is traditionally strong in server-side development, and WebSocket extends its capabilities for real-time scenarios.
SignalR — official solution from Microsoft
SignalR is a library for ASP.NET Core that abstracts working with WebSocket, providing a high-level API. It automatically selects the best transport: WebSocket, Server-Sent Events (SSE), or Long Polling. SignalR is ideal for enterprise projects where scalability and integration with the .NET ecosystem are important.
Installing SignalR
dotnet add package Microsoft.AspNetCore.SignalRExample of a simple chat on SignalR
Let's create a Hub — the central class for managing connections:
using Microsoft.AspNetCore.SignalR;
public class ChatHub : Hub{ public async Task SendMessage(string user, string message) { await Clients.All.SendAsync("ReceiveMessage", user, message); }}Configuration in Program.cs:
var builder = WebApplication.CreateBuilder(args);builder.Services.AddSignalR();
var app = builder.Build();app.MapHub<ChatHub>("/chat");
app.Run();Client in JavaScript:
const connection = new signalR.HubConnectionBuilder() .withUrl("/chat") .build();
connection.on("ReceiveMessage", (user, message) => { console.log(`${user}: ${message}`);});
connection.start().catch(err => console.error(err));Advantages of SignalR:
- Automatic transport selection (WebSocket, SSE, Long Polling)
- Support for group broadcasts and individual users
- Built-in reconnection handling on disconnect
- Integration with ASP.NET Core Identity authentication
- Scaling via Redis Backplane or Azure SignalR Service
Disadvantages:
- Heavyweight for simple projects
- Dependency on ASP.NET Core (does not work in console applications)
- Complexity in configuration for non-standard scenarios
WebSocketSharp — lightweight library for .NET Framework and .NET Core
WebSocketSharp is a minimalist library that implements the WebSocket protocol without unnecessary abstractions. It is suitable for creating both server and client WebSocket connections. Ideal for embedding in console applications or Unity games.
Installation via NuGet
dotnet add package WebSocketSharpExample of a WebSocket server
using WebSocketSharp;using WebSocketSharp.Server;
public class Echo : WebSocketBehavior{ protected override void OnMessage(MessageEventArgs e) { Send($"Echo: {e.Data}"); }}
class Program{ static void Main() { va Also in library
MailKit for C# (.NET): Complete Guide with Examples
Working with XML in JavaScript (Node.js): Parsing, Creation, and Validation
Prometheus metrics in Node.js: prom-client library