All Projects → LadislavBohm → socket.io-client-core

LadislavBohm / socket.io-client-core

Licence: MIT license
High-Performance Socket.IO client in C#

Programming Languages

C#
18002 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to socket.io-client-core

SocketIOSharp
C# implementation of Socket.IO protocol revision 4 client and server.
Stars: ✭ 101 (+44.29%)
Mutual labels:  socket-io, socket-io-client, socketio, socketio-client
Node Decorators
node-decorators
Stars: ✭ 230 (+228.57%)
Mutual labels:  socket, socket-io, socketio
VPSocketIO
socket.io client objective-c
Stars: ✭ 18 (-74.29%)
Mutual labels:  socket-io, socketio, socketio-client
SocketIOUnity
A Wrapper for socket.io-client-csharp to work with Unity.
Stars: ✭ 69 (-1.43%)
Mutual labels:  socket, socket-io, socketio
titanium-socketio
Use the native Socket.io SDK's with Axway Titanium.
Stars: ✭ 25 (-64.29%)
Mutual labels:  socket-io, socketio, socketio-client
Bizsocket
异步socket,对一些业务场景做了支持
Stars: ✭ 469 (+570%)
Mutual labels:  socket, socket-io, socketio
Angular Ngrx Socket Frontend
Angular Ngrx Socket.IO Example
Stars: ✭ 177 (+152.86%)
Mutual labels:  socket, socketio
Ngx Socket Io
Socket.IO module for Angular
Stars: ✭ 178 (+154.29%)
Mutual labels:  socket, socket-io
Socket Controllers
Use class-based controllers to handle websocket events.
Stars: ✭ 191 (+172.86%)
Mutual labels:  socket, socket-io
Web Socket
Laravel library for asynchronously serving WebSockets.
Stars: ✭ 225 (+221.43%)
Mutual labels:  socket, socket-io
React Socket Io
A react provider for socket.io, http://socket.io/
Stars: ✭ 111 (+58.57%)
Mutual labels:  socket, socketio
Oksocket
An blocking socket client for Android applications.
Stars: ✭ 2,359 (+3270%)
Mutual labels:  socket, socket-io
RRQMSocket
TouchSocket是.Net(包括 C# 、VB.Net、F#)的一个整合性的、超轻量级的网络通信框架。包含了 tcp、udp、ssl、http、websocket、rpc、jsonrpc、webapi、xmlrpc等一系列的通信模块。一键式解决 TCP 黏分包问题,udp大数据包分片组合问题等。使用协议模板,可快速实现「固定包头」、「固定长度」、「区间字符」等一系列的数据报文解析。
Stars: ✭ 286 (+308.57%)
Mutual labels:  socket, socket-io
Nuxt Socket Io
Nuxt Socket IO - socket.io client and server module for Nuxt
Stars: ✭ 148 (+111.43%)
Mutual labels:  socket, socket-io
Sample Chat Electron
Socket.io based chat server and clients, implemented in NodeJS and distributed to Windows and MacOS.
Stars: ✭ 116 (+65.71%)
Mutual labels:  socket, socket-io
Vuesocial
something like QQ、weibo、weChat(vue+express+socket.io仿微博、微信的聊天社交平台)
Stars: ✭ 189 (+170%)
Mutual labels:  socket, socket-io
Graphql Live Query
Realtime GraphQL Live Queries with JavaScript
Stars: ✭ 112 (+60%)
Mutual labels:  socket, socket-io
realtime-geolocation
Geolocation tracking app with Node.js, Socket.io, & AngularJS
Stars: ✭ 29 (-58.57%)
Mutual labels:  socket-io, socketio
socket.io-react
A High-Order component to connect React and Socket.io easily
Stars: ✭ 67 (-4.29%)
Mutual labels:  socket, socket-io
redparty
Host Youtube watch party with friends. Sync videos and chat in real-time
Stars: ✭ 70 (+0%)
Mutual labels:  socket, socket-io

socket.io client written in C#

High-performance C# client for socket.io. Flexibility achieved via Reactive Extensions and optional configurations. Built on top of .NET Standard 2.1.

Installation

PM> Install-Package Socket.Io.Client.Core

Features

  • Fully async and non-blocking flexible API
  • Emit events and receive optional callbacks
  • Subscribe/Unsubscribe to events
  • Monitor socket state
    • React to various socket high/low-level events

Changelog

  • 1.1.0
    • moved from Utf8Json to System.Text.Json (breaking change)
    • fixed parsing of multiple arguments where some of them were not JSON serialized
  • 1.2.0
    • added support for event message acknowledgements

Examples

For more examples see test project.

Real-world example can be found in my other repository - Crypto Compare streamer API client: https://github.com/LadislavBohm/cryptocompare-streamer

Open/Close Connection

//socket is disposed using new "using" syntax, don't forget to dispose it!
using var client = new SocketIoClient();
//optionally supply additional parameters using OpenOptions
var options = new SocketIoOpenOptions("custom-path");

await client.OpenAsync(new Uri("http://localhost:3000"), options);
await client.CloseAsync();

Subscribe to built-in events

Events are implemented using Reactive Extensions, for more information see here or a very nice tutorial. However a very basic usage will be shown here.

Basic subscriptions

using var client = new SocketIoClient();

//no need to hold reference to subscription as it
//will be automatically unsubscribed once client is disposed
client.Events.OnOpen.Subscribe((_) =>
{
    Console.WriteLine("Socket has been opened");
});

//subscribe to event with data
client.Events.OnPacket.Subscribe(packet =>
{
    Console.WriteLine($"Received packet: {packet}");
});

await client.OpenAsync(new Uri("http://localhost:3000"));

Time-based subscriptions

using var client = new SocketIoClient();

//in reality you shouldn't need to work directly with packets
var subscription = client.Events.OnPacket.Subscribe(packet =>
{
    Console.WriteLine($"Received packet: {packet}");
});

await client.OpenAsync(new Uri("http://localhost:3000"));
//let the subscription live for 500 milliseconds
await Task.Delay(500);
//unsubscribe from this event (socket and other subscriptions are still running)
subscription.Dispose();

Subscribe/Unsubscribe to custom events

Event data from socket.io are received as an array. In event data object you can access the whole array or for convenience just the first item (if available).

using var client = new SocketIoClient();

//in this example we throttle event messages to 1 second
var someEventSubscription = client.On("some-event")
    .Throttle(TimeSpan.FromSeconds(1)) //optional
    .Subscribe(message =>
{
    Console.WriteLine($"Received event: {message.EventName}. Data: {message.FirstData}");
});

await client.OpenAsync(new Uri("http://localhost:3000"));

//optionally unsubscribe (equivalent to off() from socket.io)
someEventSubscription.Dispose();

Acknowledgements

You can optionally send ACK to server when you receive a message. There is also option to send any data back to server.

using var client = new SocketIoClient();

client.On("messages").Subscribe(e =>
{
    //always check before calling acknowledgement
    if (e.SupportsAcknowledge)
    {
        //without any data
        e.Acknowledge();

        //OR with any optional serializable data
        e.Acknowledge("message has been processed");
    }
});

Emit message to server

All emitted messages have an optional callback (acknowledgement) possible via subscribing to the result of Emit method. All operations are non-blocking and asynchronously handled by underlying Channel library.

using var client = new SocketIoClient();

client.Emit("some-event"); //no data emitted
client.Emit("some-event", "some-data"); //string data
client.Emit("some-event", new {data = "some-data"}); //object data

//with callback (acknowledgement)
//it is always called only once, no need to unsubscribe/dispose
client.Emit("some-event", "some-data").Subscribe(ack =>
{
    Console.WriteLine($"Callback with data: {ack.Data[0]}.");
});

Configuration

//optionally supply your own implementation of ILogger (default is NullLogger)
var options = new SocketIoClientOptions()
    .With(logger: new NullLogger<SocketIoClient>());

using var client = new SocketIoClient(options);

To-do

  • Binary data (no support yet)
  • Implement automatic reconnection (you can do it by yourself using currently available events)
  • Document source code

Inspiration and Thanks

Thanks to following people and libraries that I used as an inspiration or help during development.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].