NatsWebSocket 1.2.2

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

NatsWebSocket

NATS client over WebSocket for .NET Framework 4.6.2+ and .NET Standard 2.0.

Fills the gap where the official NATS.Client v1.x has no WebSocket support and NATS.Net v2.x requires .NET 6+.

Features

  • WebSocket transport (ws:// and wss://)
  • Publish, Subscribe, Request-Reply
  • NATS headers (HPUB/HMSG)
  • NKEY + JWT authentication (.creds file support)
  • Token and user/password authentication
  • Automatic reconnection with exponential backoff + jitter
  • PING keep-alive with configurable interval and max missed PONGs
  • Single inbox optimization for request-reply
  • Zero JSON dependencies (minimal internal JSON reader/writer)
  • Only dependency: BouncyCastle.Cryptography (for Ed25519 NKEY signing)

Installation

dotnet add package NatsWebSocket

Or reference the project directly:

<ProjectReference Include="path/to/NatsWebSocket.csproj" />

Quick Start

using NatsWebSocket;
using NatsWebSocket.Auth;

var conn = new NatsConnection(new NatsConnectionOptions
{
    Url = "wss://my-nats-server:443",
    AuthHandler = new NKeyAuthHandler("path/to/creds"),
});

await conn.ConnectAsync();

// Request-Reply
var headers = new NatsHeaders();
headers.Add("token", jwt);
var reply = await conn.RequestAsync("svc.user.create", payload, headers);
Console.WriteLine(reply.GetString());

// Subscribe (async)
var sub = await conn.SubscribeAsync("notifications.>", msg =>
    Console.WriteLine($"[{msg.Subject}] {msg.GetString()}"));

// Publish
await conn.PublishAsync("events.update", data);

// Cleanup
sub.Dispose();
await conn.CloseAsync(); // always call CloseAsync before Dispose for graceful shutdown
conn.Dispose();

Authentication

NKEY + JWT (.creds file)

var auth = new NKeyAuthHandler("path/to/file.creds");

NKEY + JWT (raw values)

var auth = new NKeyAuthHandler(jwtString, nkeySeedBytes);

Token

var auth = new TokenAuthHandler("my-secret-token");

User/Password

var auth = new UserPasswordAuthHandler("user", "pass");

Connection Options

Option Default Description
Url (required) WebSocket URL (ws:// or wss://)
AuthHandler null Authentication handler
Name "NatsWebSocket" Client name in CONNECT
ConnectTimeout 10s Handshake timeout
RequestTimeout 30s Default request-reply timeout
AllowReconnect true Auto-reconnect on disconnect
MaxReconnectAttempts -1 (unlimited) Max reconnect tries
ReconnectDelay 1s Initial reconnect delay
MaxReconnectDelay 30s Maximum reconnect delay
ReconnectJitter true +/-25% random jitter
Headers true Advertise header support
NoResponders true Request 503 status headers
ReceiveBufferSize 65536 WebSocket receive buffer
PingInterval 30s Keep-alive PING interval
MaxPingOut 3 Max missed PONGs before disconnect

Events

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

Shutdown

Always call CloseAsync() before Dispose(). CloseAsync performs a graceful WebSocket close handshake and waits for background tasks to exit. Dispose is a synchronous safety net that tears down resources without waiting.

await conn.CloseAsync();
conn.Dispose();

Build

dotnet build
dotnet test
dotnet pack -c Release

Target Framework

The library targets netstandard2.0, making it compatible with:

  • .NET Framework 4.6.2+
  • .NET Core 2.0+
  • .NET 5+

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.2.2 169 2/6/2026
1.2.0 126 2/6/2026
1.0.0 126 2/3/2026