Websocket.Client
5.1.2
dotnet add package Websocket.Client --version 5.1.2
NuGet\Install-Package Websocket.Client -Version 5.1.2
<PackageReference Include="Websocket.Client" Version="5.1.2" />
<PackageVersion Include="Websocket.Client" Version="5.1.2" />
<PackageReference Include="Websocket.Client" />
paket add Websocket.Client --version 5.1.2
#r "nuget: Websocket.Client, 5.1.2"
#addin nuget:?package=Websocket.Client&version=5.1.2
#tool nuget:?package=Websocket.Client&version=5.1.2
Websocket .NET client

This is a wrapper over native C# class ClientWebSocket
with built-in reconnection and error handling.
License:
MIT
Features
- installation via NuGet (Websocket.Client)
- targeting .NET Standard 2.0 (.NET Core, Linux/MacOS compatible) + Standard 2.1, .NET 5 and .NET 6
- reactive extensions (Rx.NET)
- integrated logging abstraction (LibLog)
- using Channels for high performance sending queue
Usage
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!
Advanced configuration
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.
Change URL on the fly
It is possible to change the remote server URL dynamically. Example:
client.Url = new Uri("wss://my_new_url");;
await client.Reconnect();
Reconnecting
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}")
});
Multi-threading
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:
Default behavior
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 -----
Parallel subscriptions
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
Parallel subscriptions with synchronization
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 ----
Async/Await integration
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.
Available for help
I do consulting, please don't hesitate to contact me if you need a paid help
(web, nostr, m@mkotas.cz)
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. 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 is compatible. 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 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | 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.1
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.IO.RecyclableMemoryStream (>= 3.0.0)
- System.Reactive (>= 6.0.0)
- System.Threading.Channels (>= 8.0.0)
-
net6.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.IO.RecyclableMemoryStream (>= 3.0.0)
- System.Reactive (>= 6.0.0)
- System.Threading.Channels (>= 8.0.0)
-
net7.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.IO.RecyclableMemoryStream (>= 3.0.0)
- System.Reactive (>= 6.0.0)
- System.Threading.Channels (>= 8.0.0)
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.IO.RecyclableMemoryStream (>= 3.0.0)
- System.Reactive (>= 6.0.0)
- System.Threading.Channels (>= 8.0.0)
NuGet packages (145)
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. |
|
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. |
|
realtime-csharp
Realtime-csharp is written as a client library for supabase/realtime. |
|
Hypar.Client
The Hypar client. |
|
Blazor.Ninja.Client.Local
Blazor.Ninja Local Client Package |
GitHub repositories (23)
Showing the top 20 popular GitHub repositories that depend on Websocket.Client:
Repository | Stars |
---|---|
duplicati/duplicati
Store securely encrypted backups in the cloud!
|
|
Richasy/Bili.Uwp
适用于新系统UI的哔哩
|
|
LykosAI/StabilityMatrix
Multi-Platform Package Manager for Stable Diffusion
|
|
openbullet/OpenBullet2
OpenBullet reinvented
|
|
NethermindEth/nethermind
A robust execution client for Ethereum node operators.
|
|
ps1337/reinschauer
it is very good
|
|
andyvorld/LGSTrayBattery
A tray app used to track battery levels of wireless Logitech mouse.
|
|
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
|
|
BarRaider/obs-websocket-dotnet
C# .NET library to communicate with an obs-websocket server
|
|
not-nullptr/Aerochat
Native rewrite of Aerochat, a WLM 09 themed Discord client
|
|
floh22/LeagueBroadcast
League of Legends Spectate Overlay Tools
|
|
Kaliumhexacyanoferrat/GenHTTP
Lightweight web server written in pure C# with few dependencies to 3rd-party libraries.
|
|
androidseb25/iGotify-Notification-Assistent
Docker container for sending Gotify notifications to iOS devices (bridge to gotify websocket)
|
|
Ombrelin/plex-rich-presence
A desktop app to enable discord rich presence for your Plex Media Server Activity
|
|
Mirai-NET-Shelter/Mirai.Net
Mirai.Net是基于mirai-api-http实现的轻量级mirai社区sdk。
|
|
Azure/iotedge-lorawan-starterkit
Sample implementation of LoRaWAN components to connect LoRaWAN antenna gateway running IoT Edge directly with Azure IoT.
|
|
Hoshikawa-Kaguya/Sora
.Net 6异步机器人框架,跨平台,OneBot协议(原CQHTTP协议),在兼容协议的同时主要为Go-Cqhttp提供支持
|
|
niltor/open-pdd-net-sdk
拼多多开放平台DotNet SDK
|
|
WalletConnect/WalletConnectSharp
[Deprecated] A C# implementation of the WalletConnect protocol
|
Version | Downloads | Last updated |
---|---|---|
5.1.2 | 412,964 | 6/19/2024 |
5.1.1 | 372,254 | 2/15/2024 |
5.1.0 | 4,308 | 2/15/2024 |
5.0.0 | 243,638 | 9/7/2023 |
4.7.0 | 78,428 | 9/1/2023 |
4.6.1 | 667,683 | 2/23/2023 |
4.6.0 | 6,406 | 2/21/2023 |
4.5.2 | 36,074 | 2/20/2023 |
4.5.1 | 1,094 | 2/20/2023 |
4.5.0 | 1,703 | 2/20/2023 |
4.4.43 | 1,028,974 | 11/21/2021 |
4.4.42 | 1,992 | 11/20/2021 |
4.4.40 | 32,360 | 11/20/2021 |
4.4.39 | 1,534 | 11/19/2021 |
4.3.38 | 237,959 | 8/31/2021 |
4.3.36 | 32,579 | 8/16/2021 |
4.3.35 | 32,076 | 7/21/2021 |
4.3.32 | 105,497 | 5/24/2021 |
4.3.30 | 176,634 | 2/11/2021 |
4.3.21 | 224,614 | 7/20/2020 |
4.3.15 | 79,080 | 5/13/2020 |
4.3.14 | 1,622 | 5/12/2020 |
4.3.12 | 14,338 | 4/26/2020 |
4.2.11 | 1,350 | 4/26/2020 |
4.2.3 | 47,328 | 3/10/2020 |
4.1.85 | 21,428 | 2/14/2020 |
4.1.83 | 1,361 | 2/14/2020 |
4.1.82 | 1,339 | 2/14/2020 |
4.1.81 | 1,939 | 2/14/2020 |
4.1.79 | 1,397 | 2/14/2020 |
4.1.78 | 108,958 | 1/24/2020 |
4.1.77 | 3,723 | 1/6/2020 |
4.1.76 | 10,342 | 12/18/2019 |
4.1.75 | 1,612 | 12/14/2019 |
4.1.74 | 3,524 | 12/10/2019 |
4.1.73 | 4,372 | 12/6/2019 |
4.1.70 | 1,428 | 12/6/2019 |
4.1.69 | 1,405 | 12/6/2019 |
4.0.66 | 6,139 | 12/5/2019 |
3.2.61 | 2,314 | 12/4/2019 |
3.2.59 | 6,878 | 11/21/2019 |
3.2.56 | 25,900 | 10/2/2019 |
3.2.55 | 1,338 | 10/2/2019 |
3.2.54 | 1,335 | 10/2/2019 |
3.2.52 | 1,411 | 9/27/2019 |
3.1.32 | 1,807 | 9/20/2019 |
3.1.29 | 2,271 | 9/19/2019 |
3.1.28 | 6,474 | 8/6/2019 |
3.1.26 | 8,167 | 8/2/2019 |
3.1.25 | 7,609 | 7/24/2019 |
3.1.24 | 1,375 | 7/24/2019 |
3.0.23 | 5,034 | 6/20/2019 |
3.0.20 | 5,213 | 5/13/2019 |
3.0.19 | 2,043 | 5/2/2019 |
3.0.18 | 12,915 | 3/26/2019 |
3.0.17 | 16,800 | 3/20/2019 |
3.0.15 | 2,183 | 3/12/2019 |
2.0.10 | 4,033 | 2/4/2019 |
2.0.7 | 1,861 | 2/4/2019 |
1.0.6 | 11,100 | 12/10/2018 |
1.0.5 | 2,518 | 12/7/2018 |
1.0.4 | 2,124 | 12/7/2018 |
1.0.3 | 2,205 | 12/7/2018 |
1.0.2 | 1,804 | 11/29/2018 |
1.0.1 | 2,652 | 11/29/2018 |
Enhancements