WebSocket Libraries for C# (.NET): SignalR, WebSocket4Net, and System.Net.WebSockets Client

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

WebSocket Libraries for C# (.NET): Real-Time in Your Applications



WebSocket is a protocol that provides a persistent bidirectional connection between a client and a server. In the C# (.NET) ecosystem, there are several mature libraries for working with WebSocket, suitable for both server-side applications (ASP.NET Core) and clients (desktop, mobile, console). In this article, we will look at three key tools: SignalR (high-level framework), WebSocket4Net (cross-platform client), and the built-in System.Net.WebSockets (low-level API).



1. SignalR — Universal Real-Time Framework



SignalR is part of ASP.NET Core that abstracts working with WebSocket, Server-Sent Events, and Long Polling. It automatically selects the best transport and provides a convenient API for sending messages to groups, individual users, and all connected clients.



Installation

dotnet add package Microsoft.AspNetCore.SignalR


Server Example (Hub)

using Microsoft.AspNetCore.SignalR;

public class ChatHub : Hub{ public async Task SendMessage(string user, string message) { // Send message to all connected clients await Clients.All.SendAsync("ReceiveMessage", user, message); }

public override async Task OnConnectedAsync() { await base.OnConnectedAsync(); Console.WriteLine($"Client connected: {Context.ConnectionId}"); }

public override async Task OnDisconnectedAsync(Exception? exception) { await base.OnDisconnectedAsync(exception); Console.WriteLine($"Client disconnected: {Context.ConnectionId}"); }}


Client Example (C#)

using Microsoft.AspNetCore.SignalR.Client;

var connection = new HubConnectionBuilder() .WithUrl("https://localhost:5001/chatHub") .Build();

connection.On<string, string>("ReceiveMessage", (user, message) =>{ Console.WriteLine($"{user}: {message}");});

await connection.StartAsync();await connection.InvokeAsync("SendMessage", "Bot", "Hello, world!");


2. WebSocket4Net — Lightweight Client for .NET Framework and .NET Core



WebSocket4Net is an independent library that supports both standard WebSocket (RFC 6455) and proprietary extensions (e.g., WSS with self-signed certificates). It is especially useful for applications running on .NET Framework (Windows Forms, WPF) or in environments with limited support for modern APIs.



Installation via NuGet

Install-Package WebSocket4Net


Client Example

using WebSocket4Net;

var ws = new WebSocket("wss://echo.websocket.org");

ws.Opened += (s, e) =>{ Console.WriteLine("Connection established"); ws.Send("Hello, WebSocket!");};

ws.MessageReceived += (s, e) =>{ Console.WriteLine($"Received: {e.Message}");};

ws.Closed += (s, e) =>{ Console.WriteLine("Connection closed");};

ws.Error += (s, e) =>{ Console.WriteLine($"Error: {e.Exception.Message}");};

ws.Open();Console.ReadLine();


3. System.Net.WebSockets — Built-in Low-Level API



Starting with .NET Framework 4.5 and in all versions of .NET Core, the built-in class System.Net.WebSockets.ClientWebSocket is available. It does not require installing additional packages and gives full control over the connection, but requires manual buffer management and asynchronous operations.



Client Example Using ClientWebSocket

using System.Net.WebSockets;using System.Text;

public async Task ConnectAndSendAsync(string uri){ using var ws = new ClientWebSocket(); await ws.ConnectAsync(new Uri(uri), CancellationTok

Recommendations