SocketIOClient 4.0.0.1
dotnet add package SocketIOClient --version 4.0.0.1
NuGet\Install-Package SocketIOClient -Version 4.0.0.1
<PackageReference Include="SocketIOClient" Version="4.0.0.1" />
<PackageVersion Include="SocketIOClient" Version="4.0.0.1" />
<PackageReference Include="SocketIOClient" />
paket add SocketIOClient --version 4.0.0.1
#r "nuget: SocketIOClient, 4.0.0.1"
#:package SocketIOClient@4.0.0.1
#addin nuget:?package=SocketIOClient&version=4.0.0.1
#tool nuget:?package=SocketIOClient&version=4.0.0.1
Socket.IO-client for .NET
Languages: 中文简体 | English
An elegant socket.io client for .NET, it supports socket.io server v2/v3/v4, and has implemented http polling and websocket.
Table of Contents
Quick start
Connect to a Socket.IO server to receive and emit events.
var client = new SocketIO(new Uri("http://localhost:11400"));
client.On("event", ctx =>
{
// RawText: ["event","Hello World!", 1, {\"Name\":\"Alice\",\"Age\":18}]
// The first element in the array is the event name,
// and the subsequent elements are the data carried with the event.
Console.WriteLine(ctx.RawText);
// Use index 0 to access the first item in the data payload, which is of type string.
var message = ctx.GetValue<string>(0)!;
Console.WriteLine(message); // Hello World!
// Use index 1 to access the second data item in the payload. The data type is int.
var id = ctx.GetValue<int>(1);
Console.WriteLine(id); // 1
// Use index 2 to access the third data item in the payload. The data type is User.
var user = ctx.GetValue<User>(2)!;
Console.WriteLine($"Name: {user.Name}, Age: {user.Age}"); // Name: Alice, Age: 18
return Task.CompletedTask;
});
Options
An overload is provided here to allow custom configuration of the options.
var client = new SocketIO(new Uri("http://localhost:11400"), new SocketIOOptions
{
Query = new NameValueCollection
{
["user"] = "Alice"
},
// ...
});
| Option | Default Value | Description |
|---|---|---|
| Path | /socket.io | The service endpoint path. |
| Reconnection | true | If the initial connection attempt fails, set this to true to keep retrying until the maximum number of reconnection attempts is reached. Set it to false to fail immediately without any further retry attempts. |
| ReconnectionAttempts | 10 | When Reconnection is set to true, the client will automatically retry if the connection fails. The number of retry attempts is determined by ReconnectionAttempts. |
| ReconnectionDelayMax | 5000 | After a reconnection attempt fails, the client will wait for a random delay before trying again. ReconnectionDelayMax defines the upper bound of that random delay. |
| ConnectionTimeout | 30s | The timeout duration for each connection attempt. |
| Query | null | Before the connection is established, the client can use this parameter to send query string values to the server. |
| EIO | V4 | For Socket.IO server v2.x, please set EIO = 3. |
| ExtraHeaders | null | Send the request headers with each request to the server. These values can be used during the handshake phase. |
| Transport | Polling | By default, HTTP polling is used. When AutoUpgrade is set to true, the connection will automatically upgrade to WebSocket when possible. If the server only supports WebSocket, set Transport = TransportProtocol.WebSocket. |
| AutoUpgrade | true | After the handshake, if the server supports WebSocket while the client is currently using polling, the connection will be upgraded to WebSocket. |
| Auth | null | Configure connection credentials. This feature is not supported when EIO = 3. |
Ack
Emit an event with a callback function. After the server finishes processing, it will invoke the client’s callback function and pass back the relevant data.
Client
await client.EmitAsync("hi", ["Hi, I'm Client"], ack =>
{
Console.WriteLine(ack.RawText);
// RawText: ["Hi, I'm Client","Hi, I'm Server"]
var message1 = ack.GetValue<string>(0)!;
Console.WriteLine(message1); // Hi, I'm Client
var message2 = ack.GetValue<string>(1)!;
Console.WriteLine(message2); // Hi, I'm Server
return Task.CompletedTask;
});
Server
socket.on('hi', (m1, fn) => {
fn(m1, 'Hi, I\'m Server');
});
Listen for an event that includes a callback function. After the client finishes processing, it will send the relevant data back to the server. Once the server receives the data, it will execute its callback function.
Client
client.On("add", async ctx =>
{
var a = ctx.GetValue<int>(0);
var b = ctx.GetValue<int>(1);
var c = a + b;
await ctx.SendAckDataAsync([c]);
});
Server
socket.emit('add', 1, 2, c => {
console.log(c); // 3
});
Binary messages
Send and receive complex data types that include byte[]. By default, System.Text.Json is used for JSON serialization.
class FileDTO
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("mimeType")]
public string MimeType { get; set; }
[JsonPropertyName("bytes")]
public byte[] Bytes { get; set; }
}
The above example uses [JsonPropertyName] to customize JSON property names. Global configuration via JsonSerializerOptions is also supported.
await client.EmitAsync("1:emit", [
new FileDTO
{
Name = "template.html",
MimeType = "text/html",
Bytes = Encoding.UTF8.GetBytes("<div>test</div>")
}
]);
client.On("new files", ctx =>
{
// RawText: ["new files", {"name":"template.html","mimeType":"text/html","bytes":{"_placeholder":true,"num":0}}]
var result = ctx.GetValue<FileDTO>();
Console.WriteLine(Encoding.UTF8.GetString(result.Bytes))
});
Serializer
By default, System.Text.Json is used for serialization and deserialization. If you want to configure JsonSerializerOptions:
var client = new SocketIO(new Uri("http://localhost:11400"), services =>
{
services.AddSystemTextJson(new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
});
Of course, if you prefer to use Newtonsoft.Json, you need to install SocketIOClient.Serializer.NewtonsoftJson.
var client = new SocketIO(new Uri("http://localhost:11400"), services =>
{
services.AddNewtonsoftJson(new JsonSerializerSettings());
});
Self-signed certificate
If your socket.io server uses a certificate issued by a trusted CA, you should not use these APIs to avoid potential security risks.
However, if your socket.io server uses a self-signed certificate, you may need to customize the certificate validation logic:
Http Polling
var client = new SocketIO(new Uri("http://localhost:11400"), services =>
{
services.AddSingleton<HttpClient>(_ =>
{
var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (s, cert, chain, policyError) =>
{
var isValid = ...
return isValid;
}
};
return new HttpClient(handler);
});
});
WebSocket
var client = new SocketIO(new Uri("http://localhost:11400"), services =>
{
services.AddSingleton(new WebSocketOptions
{
RemoteCertificateValidationCallback = (s, cert, chain, policyError) =>
{
var isValid = ...
return isValid;
};
});
});
Note: By default, the client communicates with the server using polling first. If the server supports WebSocket, the connection will be upgraded to a WebSocket channel. In this scenario, you may need to configure CertificateValidationCallback for both transports.
Proxy
In some network environments, a proxy must be configured in order to communicate with the server. Proxy can also be used for development and debugging purposes to capture and inspect the entire interaction.
Http Polling
var client = new SocketIO(new Uri("http://localhost:11400"), services =>
{
services.AddSingleton<HttpClient>(_ =>
{
var handler = new HttpClientHandler
{
Proxy = new WebProxy(proxyUrl),
UseProxy = true
};
return new HttpClient(handler);
});
});
WebSocket
var client = new SocketIO(new Uri("http://localhost:11400"), services =>
{
services.AddSingleton(new WebSocketOptions
{
Proxy = new WebProxy(proxyUrl)
});
});
Note: By default, the client communicates with the server using polling first. If the server supports WebSocket, the connection will be upgraded to a WebSocket channel. In this case, you may need to configure a proxy for both transports.
Development
This library currently follows the software testing pyramid model, with 95% unit test coverage. You can view the coverage details in the Code Coverage section of the Azure DevOps interface.
If you need to run the integration tests locally:
cd socket.io-client-csharp/tests/socket.io
npm run install-all # Install the dependencies. This only needs to be done once.
npm run start # Start socket.io server for integration testing
Change log
[4.0.0] - 2026-01-28
Architecture Refactor
- Reworked the internal architecture to improve modularity, maintainability, and long-term extensibility
- Clearer separation of responsibilities between core components
- Reduced coupling between modules, making future enhancements safer and easier
Performance Enhancements
- Improved execution efficiency in key processing paths
- Reduced unnecessary allocations and redundant operations
- Optimized data access and internal workflows for better runtime performance
After that, you can run the integration tests.
Thanks
<img src="https://socket.io/images/logo.svg" width=100px/> <img src="https://github.com/darrachequesne.png" width=100px/>
Thank socket.io and darrachequesne for sponsoring the project on Open Collective.
We would like to thank JetBrains for supporting the project with free licenses of their fantastic tools.
| 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 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. 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
- Microsoft.Extensions.DependencyInjection (>= 10.0.2)
- Microsoft.Extensions.Logging (>= 10.0.2)
- SocketIOClient.Common (>= 4.0.0)
- SocketIOClient.Serializer (>= 4.0.0.1)
- SocketIOClient.Serializer.NewtonsoftJson (>= 4.0.0.1)
- System.Net.Http (>= 4.3.4)
- System.Text.Json (>= 10.0.2)
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.2)
- Microsoft.Extensions.Logging (>= 10.0.2)
- SocketIOClient.Common (>= 4.0.0)
- SocketIOClient.Serializer (>= 4.0.0.1)
- SocketIOClient.Serializer.NewtonsoftJson (>= 4.0.0.1)
- System.Text.Json (>= 10.0.2)
NuGet packages (45)
Showing the top 5 NuGet packages that depend on SocketIOClient:
| Package | Downloads |
|---|---|
|
ElectronNET.API
Building cross platform electron based desktop apps with .NET Core and ASP.NET Core. This package contains the API to access the "native" electron API. |
|
|
SocketIOClient.Newtonsoft.Json
socket.io-client json serialization/deserialization for Newtonsoft.Json |
|
|
DigitalRuby.ExchangeSharp
ExchangeSharp is a C# API for working with various cryptocurrency exchanges. Web sockets are also supported for some exchanges. |
|
|
gomake-common-library
Package Description |
|
|
Backendless.NET
Backendless is a cloud-based serverless platform which significantly simplifies the process of development of web and mobile applications. This SDK provides the client-side APIs which you can use in your app to take advantage of the Backendless platform functionality. Your application can rely on Backendless for the following functions: Backendless has a ton of awesome functionality including: - Real-time Database - Real-time Publish/Subscribe Messaging - User Management (user registration, login, logout, password recovery) - Push Notification - File operations (upload, download, copy, move, delete) - Geo location All the APIs can be secured with roles-based security. Also, you can create your own API services and deploy them into Backendless. |
GitHub repositories (9)
Showing the top 9 popular GitHub repositories that depend on SocketIOClient:
| Repository | Stars |
|---|---|
|
ElectronNET/Electron.NET
:electron: Build cross platform desktop apps with ASP.NET Core (Razor Pages, MVC, Blazor).
|
|
|
ArduPilot/MissionPlanner
Mission Planner Ground Control Station for ArduPilot (c# .net)
|
|
|
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.
|
|
|
DigitalRuby/ExchangeSharp
ExchangeSharp is a powerful, fast and easy to use .NET/C# API for interfacing with many crypto currency exchanges. REST and web sockets are supported.
|
|
|
thanhkeke97/RSTGameTranslation
🎮 Real-time Game Translation Tool | OCR + AI Translation | Windows Gaming | Open Source
|
|
|
automuteus/amonguscapture
Capture of the local Among Us executable state
|
|
|
DeSinc/SallyBot
AI Chatbot coded in Discord.net C#
|
|
|
jensroth-git/WinLaunch
macOS Launchpad for Windows
|
|
|
songify-rocks/Songify
A simple tool that gets the current track from Spotify, YouTube and Nightbot.
|
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 4.0.0.1 | 89 | 2/2/2026 | |
| 4.0.0 | 116 | 1/31/2026 | |
| 3.1.2 | 358,303 | 8/18/2024 | |
| 3.1.1 | 207,801 | 9/5/2023 | |
| 3.1.0 | 1,895 | 8/27/2023 | |
| 3.0.8 | 402,336 | 3/4/2023 | |
| 3.0.7 | 35,965 | 11/29/2022 | |
| 3.0.7-alpha.3 | 229 | 11/29/2022 | |
| 3.0.7-alpha.2 | 272 | 11/29/2022 | |
| 3.0.7-alpha.1 | 343 | 11/6/2022 | |
| 3.0.6 | 249,918 | 3/17/2022 | |
| 3.0.5 | 54,935 | 1/12/2022 | |
| 3.0.4 | 36,516 | 12/6/2021 | |
| 3.0.4-alpha.1 | 393 | 11/18/2021 | |
| 3.0.3 | 18,531 | 9/24/2021 | |
| 3.0.2 | 1,534 | 9/22/2021 | |
| 3.0.1 | 772 | 9/18/2021 | |
| 3.0.0 | 6,371 | 9/13/2021 | |
| 2.3.1 | 48,563 | 8/3/2021 | |
| 2.3.0 | 4,110 | 7/22/2021 | |
| 2.2.4 | 59,149 | 6/24/2021 | |
| 2.2.3 | 2,958 | 6/15/2021 | |
| 2.2.2 | 1,751 | 6/7/2021 | |
| 2.2.1 | 1,839 | 6/5/2021 | |
| 2.2.1-alpha.1 | 374 | 6/4/2021 | |
| 2.2.0 | 16,099 | 4/29/2021 | |
| 2.1.8-alpha-0 | 499 | 4/1/2021 | |
| 2.1.7 | 20,819 | 3/24/2021 | |
| 2.1.6 | 3,969 | 3/11/2021 | |
| 2.1.5 | 22,225 | 2/5/2021 | |
| 2.1.4 | 782 | 2/5/2021 | |
| 2.1.3 | 19,548 | 1/13/2021 | |
| 2.1.2 | 1,690 | 12/23/2020 | |
| 2.1.1 | 10,620 | 11/13/2020 | |
| 2.1.0 | 4,327 | 11/12/2020 | |
| 2.1.0-alpha.1 | 512 | 11/10/2020 | |
| 2.0.2.9 | 4,584 | 10/27/2020 | |
| 2.0.2.8 | 1,109 | 10/25/2020 | |
| 2.0.2.7 | 12,283 | 10/9/2020 | |
| 2.0.2.6 | 196,755 | 9/9/2020 | |
| 2.0.2.5 | 1,289 | 9/8/2020 | |
| 2.0.2.4 | 13,854 | 8/14/2020 | |
| 2.0.2.3 | 2,457 | 7/22/2020 | |
| 2.0.2.2 | 1,316 | 7/11/2020 | |
| 2.0.2.1 | 74,640 | 6/18/2020 | |
| 2.0.2.1-alpha.2 | 675 | 6/16/2020 | |
| 2.0.2.1-alpha.1 | 658 | 6/15/2020 | |
| 2.0.2 | 4,262 | 6/15/2020 | |
| 2.0.2-alpha.2 | 684 | 6/10/2020 | |
| 2.0.2-alpha.1 | 686 | 6/10/2020 | |
| 2.0.1 | 3,118 | 6/2/2020 | |
| 2.0.1-alpha.5 | 713 | 6/1/2020 | |
| 2.0.1-alpha.4 | 786 | 5/30/2020 | |
| 2.0.1-alpha.3 | 693 | 5/30/2020 | |
| 2.0.1-alpha.2 | 2,740 | 5/29/2020 | |
| 2.0.1-alpha.1 | 711 | 5/29/2020 | |
| 2.0.0 | 32,153 | 5/21/2020 | |
| 2.0.0-beta.3 | 687 | 5/21/2020 | |
| 2.0.0-beta.2 | 687 | 5/21/2020 | |
| 2.0.0-beta.1 | 759 | 5/18/2020 | |
| 2.0.0-alpha-2 | 851 | 5/16/2020 | |
| 2.0.0-alpha | 761 | 5/15/2020 | |
| 1.0.3.13 | 3,745 | 5/8/2020 | |
| 1.0.3.13-alpha | 853 | 5/7/2020 | |
| 1.0.3.12 | 5,877 | 4/10/2020 | |
| 1.0.3.11 | 1,094 | 4/9/2020 | |
| 1.0.3.10 | 1,068 | 4/8/2020 | |
| 1.0.3.9 | 1,315 | 4/7/2020 | |
| 1.0.3.8 | 15,817 | 3/17/2020 | |
| 1.0.3.7 | 1,102 | 3/17/2020 | |
| 1.0.3.6 | 1,115 | 3/16/2020 | |
| 1.0.3.5 | 1,049 | 3/16/2020 | |
| 1.0.3.4 | 1,019 | 3/16/2020 | |
| 1.0.3.3 | 1,645 | 3/8/2020 | |
| 1.0.3.2 | 1,030 | 3/7/2020 | |
| 1.0.3.1 | 1,043 | 3/7/2020 | |
| 1.0.3 | 1,205 | 3/2/2020 | |
| 1.0.2.7 | 1,491 | 2/13/2020 | |
| 1.0.2.6 | 2,762 | 10/21/2019 | |
| 1.0.2.5 | 1,277 | 9/14/2019 | |
| 1.0.2.4 | 1,144 | 9/8/2019 | |
| 1.0.2.3 | 1,121 | 8/27/2019 | |
| 1.0.2.2 | 1,100 | 8/26/2019 | |
| 1.0.2.1 | 1,093 | 8/21/2019 | |
| 1.0.2 | 1,157 | 8/21/2019 | |
| 1.0.1 | 1,188 | 8/7/2019 | |
| 1.0.0 | 1,690 | 8/6/2019 |