WebSocket Libraries for C# (.NET): SignalR, Fleck, and WebSocketSharp
WebSocket is a full-duplex communication protocol over TCP, allowing the server and client to exchange data in real time without constant HTTP requests. In the C# (.NET) ecosystem, there are several libraries that simplify WebSocket integration. In this article, we will look at three of the most popular solutions: SignalR (official Microsoft library), Fleck (lightweight server), and WebSocketSharp (universal client-server).
1. SignalR — Official Tool from Microsoft
SignalR is a high-level library for ASP.NET Core that automatically selects the best transport (WebSocket, Server-Sent Events, Long Polling) depending on client capabilities. It is ideal for creating chats, dashboards, and games.
Key Features:
- Automatic transport switching.
- Groups and broadcast messaging.
- Scalability support (Redis, Azure SignalR Service).
- Integration with clients on JavaScript, .NET, Java.
Example Server Hub in C#:
using Microsoft.AspNetCore.SignalR;
public class ChatHub : Hub{ public async Task SendMessage(string user, string message) { // Broadcast the message to all connected clients await Clients.All.SendAsync("ReceiveMessage", user, message); }
public override async Task OnConnectedAsync() { await Clients.Caller.SendAsync("Notify", "You have connected to the chat"); await base.OnConnectedAsync(); }}Example Client in C# (Console):
using Microsoft.AspNetCore.SignalR.Client;
var connection = new HubConnectionBuilder() .WithUrl("https://yourserver.com/chathub") .Build();
connection.On<string, string>("ReceiveMessage", (user, message) =>{ Console.WriteLine($"{user}: {message}");});
await connection.StartAsync();await connection.InvokeAsync("SendMessage", "User1", "Hello!");2. Fleck — Minimalistic WebSocket Server
Fleck is a lightweight library for creating WebSocket servers without dependencies on ASP.NET. It is ideal for simple applications where complex SignalR abstractions are not needed.
Features:
- Works on pure TCP (without HTTP).
- Supports SSL (WSS).
- No built-in scaling — only a single process.
Example Server with Fleck:
using Fleck;
var server = new WebSocketServer("ws://0.0.0.0:8181");var connections = new List<IWebSocketConnection>();
server.Start(socket =>{ socket.OnOpen = () => { Console.WriteLine("Client connected"); connections.Add(socket); }; socket.OnClose = () => { Console.WriteLine("Client disconnected"); connections.Remove(socket); }; socket.OnMessage = message => { // Send message to all foreach (var client in connections) { client.Send($"Echo: {message}"); } };});
Console.WriteLine("Server started on ws://localhost:8181");Console.ReadLine();3. WebSocketSharp — Universal Client and Server
WebSocketSharp is an open-source library that provides both client and server WebSocket functionality. It is independent of ASP.NET and works on .NET Framework and .NET Core.
Key Features:
- Support for SSL/TLS and proxies.
- Event-driven model (OnOpen, OnClose, OnMessage, OnError).
- Sending binary data (byte[]).
Example Client with WebSocketSharp:
using WebSocketSharp;
using (var ws = new WebSocket("wss://echo.websocket.org")){ ws.OnMessage += (sender, e) =>