![]() |
VOOZH | about |
dotnet add package Websocket.Client --version 5.5.0
NuGet\Install-Package Websocket.Client -Version 5.5.0
<PackageReference Include="Websocket.Client" Version="5.5.0" />
<PackageVersion Include="Websocket.Client" Version="5.5.0" />Directory.Packages.props
<PackageReference Include="Websocket.Client" />Project file
paket add Websocket.Client --version 5.5.0
#r "nuget: Websocket.Client, 5.5.0"
#:package Websocket.Client@5.5.0
#addin nuget:?package=Websocket.Client&version=5.5.0Install as a Cake Addin
#tool nuget:?package=Websocket.Client&version=5.5.0Install as a Cake Tool
👁 NuGet version
👁 Nuget downloads
👁 CI build
This is a wrapper over native C# class ClientWebSocket with built-in reconnection and error handling.
MIT
Microsoft.Extensions.Logging)Websocket.Client is designed for long-running websocket consumers where per-message allocation and reconnect behavior matter:
ArrayPool<byte> buffersSystem.Threading.Channels with a single readerRepresentative BenchmarkDotNet results show meaningful improvements on typical small and medium messages:
264.17 ns / 560 B to 36.26 ns / 280 B784.50 ns / 8496 B to 485.90 ns / 8216 B102.36 ns / 1048 B to 71.05 ns / 0 B738.40 ns / 8216 B to 419.79 ns / 0 B28.72 ns / 64 B to approximately 0 ns / 0 B31.54 ns / 24 B to 29.65 ns / 0 BResponseMessage.ToString() at 32 KB: 1.149 us / 32872 B to 44.60 ns / 104 Bnetstandard2.0 asset: 1000 x 1024 B text messages in 1.062 ms / 2312.21 KBFor very large text messages, the resulting string allocation dominates the receive cost, so the library focuses on avoiding unnecessary intermediate allocations and avoiding retention of oversized receive buffers after traffic spikes.
See the folder for the BenchmarkDotNet project, commands, and full result tables.
var exitEvent = new ManualResetEvent(false);
var url = new Uri("wss://xxx");
using (var client = new WebsocketClient(url))
{
client.ReconnectTimeout = TimeSpan.FromSeconds(30);
client.ReconnectionHappened.Subscribe(info =>
Log.Information($"Reconnection happened, type: {info.Type}"));
client.MessageReceived.Subscribe(msg => Log.Information($"Message received: {msg}"));
client.Start();
Task.Run(() => client.Send("{ message }"));
exitEvent.WaitOne();
}
More usage examples:
Pull Requests are welcome!
To set some advanced configurations, which are available on the native ClientWebSocket class,
you have to provide the factory method as a second parameter to WebsocketClient.
That factory method will be called on every reconnection to get a new instance of the ClientWebSocket.
var factory = new Func<ClientWebSocket>(() => new ClientWebSocket
{
Options =
{
KeepAliveInterval = TimeSpan.FromSeconds(5),
Proxy = ...
ClientCertificates = ...
}
});
var client = new WebsocketClient(url, factory);
client.Start();
Also, you can access the current native class via client.NativeClient.
But use it with caution, on every reconnection there will be a new instance.
It is possible to change the remote server URL dynamically. Example:
client.Url = new Uri("wss://my_new_url");;
await client.Reconnect();
A built-in reconnection invokes after 1 minute (default) of not receiving any messages from the server.
It is possible to configure that timeout via communicator.ReconnectTimeout.
In addition, a stream ReconnectionHappened sends information about the type of reconnection.
However, if you are subscribed to low-rate channels, you will likely encounter that timeout - higher it to a few minutes or implement ping-pong interaction on your own every few seconds.
In the case of a remote server outage, there is a built-in functionality that slows down reconnection requests
(could be configured via client.ErrorReconnectTimeout, the default is 1 minute).
Usually, websocket servers do not keep a persistent connection between reconnections. Every new connection creates a new session.
Because of that, you most likely need to resubscribe to channels/groups/topics inside ReconnectionHappened stream.
client.ReconnectionHappened.Subscribe(info => {
client.Send("{type: subscribe, topic: xyz}")
});
Observables from Reactive Extensions are single threaded by default. It means that your code inside subscriptions is called synchronously and as soon as the message comes from websocket API. It brings a great advantage of not to worry about synchronization, but if your code takes a longer time to execute it will block the receiving method, buffer the messages and may end up losing messages. For that reason consider to handle messages on the other thread and unblock receiving thread as soon as possible. I've prepared a few examples for you:
Every subscription code is called on a main websocket thread. Every subscription is synchronized together. No parallel execution. It will block the receiving thread.
client
.MessageReceived
.Where(msg => msg.Text != null)
.Where(msg => msg.Text.StartsWith("{"))
.Subscribe(obj => { code1 });
client
.MessageReceived
.Where(msg => msg.Text != null)
.Where(msg => msg.Text.StartsWith("["))
.Subscribe(arr => { code2 });
// 'code1' and 'code2' are called in a correct order, according to websocket flow
// ----- code1 ----- code1 ----- ----- code1
// ----- ----- code2 ----- code2 code2 -----
Every single subscription code is called on a separate thread. Every single subscription is synchronized, but different subscriptions are called in parallel.
client
.MessageReceived
.Where(msg => msg.Text != null)
.Where(msg => msg.Text.StartsWith("{"))
.ObserveOn(TaskPoolScheduler.Default)
.Subscribe(obj => { code1 });
client
.MessageReceived
.Where(msg => msg.Text != null)
.Where(msg => msg.Text.StartsWith("["))
.ObserveOn(TaskPoolScheduler.Default)
.Subscribe(arr => { code2 });
// 'code1' and 'code2' are called in parallel, do not follow websocket flow
// ----- code1 ----- code1 ----- code1 -----
// ----- code2 code2 ----- code2 code2 code2
In case you want to run your subscription code on the separate thread but still want to follow websocket flow through every subscription, use synchronization with gates:
private static readonly object GATE1 = new object();
client
.MessageReceived
.Where(msg => msg.Text != null)
.Where(msg => msg.Text.StartsWith("{"))
.ObserveOn(TaskPoolScheduler.Default)
.Synchronize(GATE1)
.Subscribe(obj => { code1 });
client
.MessageReceived
.Where(msg => msg.Text != null)
.Where(msg => msg.Text.StartsWith("["))
.ObserveOn(TaskPoolScheduler.Default)
.Synchronize(GATE1)
.Subscribe(arr => { code2 });
// 'code1' and 'code2' are called concurrently and follow websocket flow
// ----- code1 ----- code1 ----- ----- code1
// ----- ----- code2 ----- code2 code2 ----
Using async/await in your subscribe methods is a bit tricky. Subscribe from Rx.NET doesn't await tasks,
so it won't block stream execution and cause sometimes undesired concurrency. For example:
client
.MessageReceived
.Subscribe(async msg => {
// do smth 1
await Task.Delay(5000); // waits 5 sec, could be HTTP call or something else
// do smth 2
});
That await Task.Delay won't block stream and subscribe method will be called multiple times concurrently.
If you want to buffer messages and process them one-by-one, then use this:
client
.MessageReceived
.Select(msg => Observable.FromAsync(async () => {
// do smth 1
await Task.Delay(5000); // waits 5 sec, could be HTTP call or something else
// do smth 2
}))
.Concat() // executes sequentially
.Subscribe();
If you want to process them concurrently (avoid synchronization), then use this
client
.MessageReceived
.Select(msg => Observable.FromAsync(async () => {
// do smth 1
await Task.Delay(5000); // waits 5 sec, could be HTTP call or something else
// do smth 2
}))
.Merge() // executes concurrently
// .Merge(4) you can limit concurrency with a parameter
// .Merge(1) is same as .Concat() (sequentially)
// .Merge(0) is invalid (throws exception)
.Subscribe();
More info on Github issue.
Don't worry about websocket connection, those sequential execution via .Concat() or .Merge(1) has no effect on receiving messages.
It won't affect receiving thread, only buffers messages inside MessageReceived stream.
But beware of producer-consumer problem when the consumer will be too slow. Here is a StackOverflow issue with an example how to ignore/discard buffered messages and always process only the last one.
I do consulting, please don't hesitate to contact me if you need a paid help
(web, nostr, )
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 is compatible. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on Websocket.Client:
| Package | Downloads |
|---|---|
|
ZoomNet
ZoomNet is a strongly typed .NET client for Zoom's API. |
|
|
Supabase.Realtime
Realtime-csharp is written as a client library for supabase/realtime. |
|
|
realtime-csharp
Realtime-csharp is written as a client library for supabase/realtime. |
|
|
SlackConnector
SlackConnector initiates an open connection between you and the Slack api servers. SlackConnector uses web-sockets to allow real-time messages to be received and handled within your application. |
|
|
HiveMQtt
The HiveMQ MQTT Client is a MQTT 5.0 compatible C# library. |
Showing the top 20 popular GitHub repositories that depend on Websocket.Client:
| Repository | Stars |
|---|---|
|
duplicati/duplicati
Store securely encrypted backups in the cloud!
|
|
|
LykosAI/StabilityMatrix
Multi-Platform Package Manager for Stable Diffusion
|
|
|
Richasy/Bili.Uwp
适用于新系统UI的哔哩
|
|
|
openbullet/OpenBullet2
OpenBullet reinvented
|
|
|
NethermindEth/nethermind
A robust, high-performance execution client for Ethereum node operators.
|
|
|
visualHFT/VisualHFT
VisualHFT is a WPF/C# desktop GUI that shows market microstructure in real time. You can track advanced limit‑order‑book dynamics and execution quality, then use its modular plugins to shape the analysis to your workflow.
|
|
|
andyvorld/LGSTrayBattery
A tray app used to track battery levels of wireless Logitech mouse.
|
|
|
not-nullptr/Aerochat
Native rewrite of Aerochat, a WLM 09 themed Discord client
|
|
|
ps1337/reinschauer
it is very good
|
|
|
Ftbom/Aria2Manager
Aria2 Server Manager
|
|
|
androidseb25/iGotify-Notification-Assistent
Docker container for sending Gotify notifications to iOS devices (bridge to gotify websocket)
|
|
|
PiotrMachowski/Home-Assistant-Taskbar-Menu
This application is a simple Home Assistant client for Windows. It can display Lovelace views, control entities and show persistent notifications.
|
|
|
BruceQiu1996/NPhoenix
Base on Lcu api,support many functions.Let's go by read readme.md
|
|
|
Kaliumhexacyanoferrat/GenHTTP
Lightweight embeddable web server written in pure C# with few dependencies to 3rd-party libraries.
|
|
|
stardew-valley-dedicated-server/server
Always-on Stardew Valley dedicated multiplayer server in Docker, with automatic backups and SMAPI mod support.
|
|
|
BarRaider/obs-websocket-dotnet
C# .NET library to communicate with an obs-websocket server
|
|
|
floh22/LeagueBroadcast
League of Legends Spectate Overlay Tools
|
|
|
Ombrelin/plex-rich-presence
A desktop app to enable discord rich presence for your Plex Media Server Activity
|
|
|
the-hideout/TarkovMonitor
Monitor Tarkov log files to help track progress, queues, and groups
|
|
|
Azure/iotedge-lorawan-starterkit
Sample implementation of LoRaWAN components to connect LoRaWAN antenna gateway running IoT Edge directly with Azure IoT.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 5.5.0 | 54,407 | 5/19/2026 |
| 5.4.0 | 4,522 | 5/13/2026 |
| 5.3.0 | 857,974 | 9/26/2025 |
| 5.2.0 | 339,348 | 5/22/2025 |
| 5.1.2 | 1,266,694 | 6/19/2024 |
| 5.1.1 | 1,068,380 | 2/15/2024 |
| 5.1.0 | 6,697 | 2/15/2024 |
| 5.0.0 | 283,189 | 9/7/2023 |
| 4.7.0 | 206,199 | 9/1/2023 |
| 4.6.1 | 1,187,844 | 2/23/2023 |
| 4.6.0 | 7,436 | 2/21/2023 |
| 4.5.2 | 42,609 | 2/20/2023 |
| 4.5.1 | 1,880 | 2/20/2023 |
| 4.5.0 | 2,699 | 2/20/2023 |
| 4.4.43 | 1,308,230 | 11/21/2021 |
| 4.4.42 | 2,782 | 11/20/2021 |
| 4.4.40 | 33,688 | 11/20/2021 |
| 4.4.39 | 2,659 | 11/19/2021 |
| 4.3.38 | 267,604 | 8/31/2021 |
| 4.3.36 | 34,050 | 8/16/2021 |
Add .NET Standard 2.0 support for full .NET Framework consumers and update the .NET Framework sample.