Websocket.Client.Net 0.0.3

dotnet add package Websocket.Client.Net --version 0.0.3
                    
NuGet\Install-Package Websocket.Client.Net -Version 0.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Websocket.Client.Net" Version="0.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Websocket.Client.Net" Version="0.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Websocket.Client.Net" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Websocket.Client.Net --version 0.0.3
                    
#r "nuget: Websocket.Client.Net, 0.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Websocket.Client.Net@0.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Websocket.Client.Net&version=0.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Websocket.Client.Net&version=0.0.3
                    
Install as a Cake Tool

Websocket.Client.Net

A native WebSocket client library for .NET 8 and .NET 10. It uses only the BCL ClientWebSocket with no third-party runtime dependencies, and provides built-in support for ws://, wss://, headers, cookies, event notifications, and automatic reconnection.

Quick Start

using Websocket.Client.Net;

await using var ws = new WebSocketClient("wss://example.com/ws", new WebSocketClientOptions
{
    AutoReconnect = true,
    MaxReconnectAttempts = 10,             // -1 means unlimited retries
    ReconnectDelay = TimeSpan.FromSeconds(1)
});

ws.SetHeader("Authorization", "Bearer token");
ws.SetHeader("User-Agent", "my-service/1.0");
ws.SetCookie("session", "cookie-value");

ws.OnOpen += (sender, e) =>
{
    Console.WriteLine(e.IsReconnect ? "Reconnected" : "Connected");
};

ws.OnMessage += (sender, e) =>
{
    if (e.IsText)
        Console.WriteLine(e.Text);
    else
        Console.WriteLine($"binary: {e.Data.Length} bytes");
};

ws.OnError += (sender, e) =>
{
    Console.WriteLine($"{e.Operation}: {e.Message}; willReconnect={e.WillReconnect}");
};

ws.OnClose += (sender, e) =>
{
    Console.WriteLine($"{e.Code}: {e.Reason}; clean={e.WasClean}");
};

ws.OnReconnecting += (sender, e) =>
{
    Console.WriteLine($"Reconnect attempt {e.Attempt}; waiting {e.Delay}");
};

await ws.ConnectAsync();
await ws.SendAsync("hello");
await ws.DisconnectAsync(reason: "shutdown");

Binary sending provides explicit overloads, all of which support CancellationToken:

await ws.SendAsync(byteArray, cancellationToken: cancellationToken);
await ws.SendAsync(arraySegment, cancellationToken: cancellationToken);
await ws.SendAsync(readOnlySequence, cancellationToken: cancellationToken);

The token passed to ConnectAsync(cancellationToken) cancels only the current caller's wait; it does not cancel the connection or reconnection flow shared by other callers. If a send token is canceled after a native send has started, the current physical connection is aborted so that later messages cannot follow an incomplete WebSocket message. When automatic reconnection is enabled, a clean new connection is established.

Throughput-First Zero-Copy Receiving

To keep data valid after an event handler returns, OnMessage creates a stable byte[] for each complete message. Throughput-sensitive paths can use MessageReceivedAsync instead. The supplied memory comes from a pool, remains stable across await inside that callback, and may only be used until the callback completes:

ws.MessageReceivedAsync = async (sender, message, type, cancellationToken) =>
{
    await processor.ProcessAsync(message, cancellationToken);
    // Do not retain message beyond the callback; call message.ToArray() if needed.
};
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.3 130 8/23/2026
0.0.2 118 8/13/2026
0.0.1 125 8/12/2026