Need help with
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

291 Stars 61 Forks MIT License 467 Commits 27 Opened issues

Description implemention for .NET

Services available


Need anything else?

Contributors list

Socket.IO-client for .NET

An elegant client for .NET, it supports server v2/v3/v4, and has implemented http polling and websocket.

Build Status NuGet Target Framework NuGet

Table of Contents

Quick start

Connect to the server, listen events and emit some data.

var client = new SocketIO("http://localhost:11000/");

client.On("hi", response => { // You can print the returned data first to decide what to do next. // output: ["hi client"] Console.WriteLine(response);

string text = response.GetValue<string>();

// The server code looks like this:
// socket.emit('hi', 'hi client');


client.On("test", response => { // You can print the returned data first to decide what to do next. // output: ["ok",{"id":1,"name":"tom"}] Console.WriteLine(response);

// Get the first data in the response
string text = response.GetValue<string>();
// Get the second data in the response
var dto = response.GetValue<testdto>(1);

// The server code looks like this:
// socket.emit('hi', 'ok', { id: 1, name: 'tom'});


client.OnConnected += async (sender, e) => { // Emit a string await client.EmitAsync("hi", "");

// Emit a string and an object
var dto = new TestDTO { Id = 123, Name = "bob" };
await client.EmitAsync("register", "source", dto);

}; await client.ConnectAsync();


The way to override the default options is as follows:

var client = new SocketIO("http://localhost:11000/", new SocketIOOptions
    Query = new List>
        new KeyValuePair("token", "abc123"),
        new KeyValuePair("key", "value")

| Option | Default value | Description | | :- | :- | :- | |

| name of the path that is captured on the server side | |
| whether to reconnect automatically | |
| number of reconnection attempts before giving up | |
| how long to initially wait before attempting a new reconnection. Affected by +/-
, for example the default initial delay will be between 500 to 1500ms. | |
| 0 <= RandomizationFactor <= 1 | |
| connection timeout | |
| additional query parameters that are sent when connecting a namespace (then found in
object on the server-side) | |
| If your server is using server v2.x, please explicitly set it to 3 | |
| Headers that will be passed for each request to the server (via xhr-polling and via websockets). These values then can be used during handshake or for special proxies. | |
| Websocket is used by default, you can change to http polling. |


The server executes the client's ack function


await client.EmitAsync("ack", response =>
    // You can print the returned data first to decide what to do next.
    // output: [{"result":true,"message":"Prometheus - server"}]
    var result = response.GetValue();
}, "Prometheus");


socket.on("ack", (name, fn) => {
        result: true,
        message: `${name} - server`

The client executes the server's ack function


client.On("ack2", async response =>
    // You can print the returned data first to decide what to do next.
    // output: [1, 2]
    int a = response.GetValue();
    int b = response.GetValue(1);

await response.CallbackAsync(b, a);



socket.emit("ack2", 1, 2, (arg1, arg2) => {
    console.log(`arg1: ${arg1}, arg2: ${arg2}`);

The output of the server is:

arg1: 2, arg2: 1

Binary messages

This example shows how to emit and receive binary messages, The library uses System.Text.Json to serialize and deserialize json by default.

class FileDTO
    public string Name { get; set; }

public string MimeType { get; set; }

public byte[] Bytes { get; set; }


client.OnConnected += async (sender, e) =>
    await client.EmitAsync("upload", new FileDTO
        Name = "template.html"
        MimeType = "text/html",
        bytes = Encoding.UTF8.GetBytes("
") }); };

client.On("new files", response => { // You can print the returned data first to decide what to do next. // output: [{"name":"template.html","mimeType":"text/html","bytes":{"_placeholder":true,"num":0}}] Console.WriteLine(response); var result = response.GetValue(); Console.WriteLine(Encoding.UTF8.GetString(result.Bytes)) });


The library uses System.Text.Json to serialize and deserialize json by default, If you want to change JsonSerializerOptions, you can do this:

var client = new SocketIO("http://localhost:11000/");
var jsonSerializer = socket.JsonSerializer as SystemTextJsonSerializer;
jsonSerializer.OptionsProvider = () => new JsonSerializerOptions
    PropertyNameCaseInsensitive = true

Of course you can also use Newtonsoft.Json library, for this, you need to install

var jsonSerializer = new NewtonsoftJsonSerializer();
jsonSerializer.OptionsProvider = () => new JsonSerializerSettings
    ContractResolver = new DefaultContractResolver
        NamingStrategy = new CamelCaseNamingStrategy()
socket.JsonSerializer = jsonSerializer;

ClientWebSocket Options

You can set proxy and add headers for WebSocket client, etc.

var client = new SocketIO("http://localhost:11000/");
client.ClientWebSocketProvider = () =>
    var clientWebSocket = new DefaultClientWebSocket
        ConfigOptions = o =>
            var options = o as ClientWebSocketOptions;

        var proxy = new WebProxy("");
        proxy.Credentials = new NetworkCredential("username", "password");
        options.Proxy = proxy;

        options.SetRequestHeader("key", "value");

        options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =&gt;
            Console.WriteLine("SslPolicyErrors: " + sslPolicyErrors);
            if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
                return true;
            return true;
return clientWebSocket;


Windows 7 Support

The library uses System.Net.WebSockets.ClientWebSocket by default. Unfortunately, it does not support Windows 7 or Windows Server 2008 R2. You will get a PlatformNotSupportedException. To solve this problem, you need to install the

dependency and then change the implementation of ClientWebSocket.
client.ClientWebSocketProvider = () => new ClientWebSocketManaged();


The library will always try to connect to the server, and an exception will be thrown when the connection fails. The library catches some exception types, such as: TimeoutException, WebSocketException, HttpRequestException and OperationCanceledException. If it is one of them, the library will continue to try to connect to the server. If there are other exceptions, the library will stop reconnecting and throw exception to the upper layer. You need extra attention in Xamarin.

For Xamarin.Android you should add the following code:

    public partial class MainPage: ContentPage
        public MainPage()

    public SocketIO Socket {get;}


public class MainActivity: global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    protected override void OnCreate(Bundle savedInstanceState)
        var app = new App();
        var mainPage = app.MainPage as MainPage;


I don't have a macOS device, and I don't know the specific exceptions of Xamarin.iOS. Welcome to create a pr and update this document. thanks :)

Breaking changes

Breaking changes in 3.0.0

While WebSocket is clearly the best way to establish a bidirectional communication, experience has shown that it is not always possible to establish a WebSocket connection, due to corporate proxies, personal firewall, antivirus software…

  • SocketIOClient v3.x supports http polling, but if websocket is available, the library will choose to use websocket. If you want to use http polling and do not want the library to upgrade the transport, please set
    Options.AutoUpgrade = false
  • server v2.x is no longer supported. If a large number of users use this version, please feedback.

Specific break changes

1. EIO option removed

Since server v2 is not supported, the EIO option is not required.

2. Removed the 'Socket' object

Use ClientWebSocketProvider instead of Socket object.

Breaking changes in 2.2.4

Before SocketIOClient v2.2.4, the default EIO is 3, which works with v2.x, in SocketIOClient v2.2.4, the default EIO is 4, which works with v3.x and v4.x

Breaking changes in 2.2.0

SocketIOClient v2.2.0 makes

the default JSON serializer. If you'd like to continue to use
, add the SocketIOClient.Newtonsoft.Json NuGet package and set your JsonSerializer to NewtonsoftJsonSerializer on your SocketIO instance. System.Text.Json is faster and uses less memory.

Change log



We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.