NatsWebSocket 1.2.2
dotnet add package NatsWebSocket --version 1.2.2
NuGet\Install-Package NatsWebSocket -Version 1.2.2
<PackageReference Include="NatsWebSocket" Version="1.2.2" />
<PackageVersion Include="NatsWebSocket" Version="1.2.2" />
<PackageReference Include="NatsWebSocket" />
paket add NatsWebSocket --version 1.2.2
#r "nuget: NatsWebSocket, 1.2.2"
#:package NatsWebSocket@1.2.2
#addin nuget:?package=NatsWebSocket&version=1.2.2
#tool nuget:?package=NatsWebSocket&version=1.2.2
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 | Versions 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. |
-
.NETStandard 2.0
- BouncyCastle.Cryptography (>= 2.6.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.