WebSocketFlow 2.0.0-alpha.1

This is a prerelease version of WebSocketFlow.
dotnet add package WebSocketFlow --version 2.0.0-alpha.1
                    
NuGet\Install-Package WebSocketFlow -Version 2.0.0-alpha.1
                    
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="WebSocketFlow" Version="2.0.0-alpha.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WebSocketFlow" Version="2.0.0-alpha.1" />
                    
Directory.Packages.props
<PackageReference Include="WebSocketFlow" />
                    
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 WebSocketFlow --version 2.0.0-alpha.1
                    
#r "nuget: WebSocketFlow, 2.0.0-alpha.1"
                    
#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 WebSocketFlow@2.0.0-alpha.1
                    
#: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=WebSocketFlow&version=2.0.0-alpha.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=WebSocketFlow&version=2.0.0-alpha.1&prerelease
                    
Install as a Cake Tool

WebSocketFlow

CI NuGet Version NuGet Downloads License: MIT

English | 简体中文

Extension methods for System.Net.WebSockets.WebSocket that simplify receiving complete messages and asynchronously enumerating incoming messages.


Features

  • ReceiveMessageAsync — Receives a complete WebSocket message, automatically assembling all fragments into a single WebSocketMessage.
  • ReadAllMessagesAsync — Returns an IAsyncEnumerable<WebSocketMessage> that yields complete messages until the socket closes or a cancellation token is triggered.

Target Frameworks

Framework Supported
.NET 10.0+ ✓

Installation

dotnet add package WebSocketFlow

Usage

Receive a single message

using WebSocketFlow;

// Default buffer size (4096 bytes)
WebSocketMessage message = await webSocket.ReceiveMessageAsync(cancellationToken);

// Custom buffer size
WebSocketMessage message = await webSocket.ReceiveMessageAsync(bufferSize: 8192, cancellationToken);

Console.WriteLine(Encoding.UTF8.GetString(message.Data.Span));

Enumerate all messages

using WebSocketFlow;

await foreach (var msg in webSocket.ReadAllMessagesAsync(cancellationToken: cancellationToken))
{
    Console.WriteLine(Encoding.UTF8.GetString(msg.Data.Span));
}

// Custom buffer size
await foreach (var msg in webSocket.ReadAllMessagesAsync(bufferSize: 8192, cancellationToken))
{
    Console.WriteLine(Encoding.UTF8.GetString(msg.Data.Span));
}

Enumeration stops automatically when a close frame is received, the cancellation token is triggered, the socket is aborted, or the remote side drops the connection (or stays silent for 5 seconds, configurable, without starting to send) after you already sent your own close frame. Any other WebSocketException, including a premature disconnect, propagates to the caller. The library does not complete the closing handshake; call CloseOutputAsync/CloseAsync yourself. After a silent-peer timeout the closing handshake did not complete, so call Abort() if you do not need it to; the timeout is accurate to within one second.

Limits

Messages are limited to 16 MiB by default. A larger message sends a MessageTooBig close frame (best effort, 5 s timeout by default), aborts the socket and throws InvalidDataException; pass maxMessageSize to change the limit. bufferSize must be between 1 byte and 1 MiB, and more than 1024 consecutive empty non-final fragments are rejected the same way (with a PolicyViolation close frame).

var message = await webSocket.ReceiveMessageAsync(bufferSize: 8192, maxMessageSize: 1_000_000, cancellationToken);
await foreach (var msg in webSocket.ReadAllMessagesAsync(8192, 1_000_000, cancellationToken)) { }

// Optional close timeout (default 5 s; Timeout.InfiniteTimeSpan disables it)
await foreach (var msg in webSocket.ReadAllMessagesAsync(8192, 1_000_000, TimeSpan.FromSeconds(10), cancellationToken)) { }

WebSocketMessage

Member Type Description
MessageType WebSocketMessageType Text, Binary, or Close
Data ReadOnlyMemory<byte> Fully assembled payload
CloseReceived bool true when MessageType == Close
GetString() string Decodes the payload as UTF-8
Product Compatible and additional computed target framework versions.
.NET 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.

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
2.0.0-alpha.1 36 10/29/2026
1.0.3 145 5/26/2026
1.0.1 136 4/17/2026
1.0.0 165 4/17/2026