Neo Socket

Published on
6 mins read
––– views
Demo

NeoSocket: A High-Performance Framework for Real-Time Applications

Overview

  • Purpose: Crafted for applications demanding top-notch performance and scalability such as MMORPG and Metaverse development, plus its flexibility also makes it a solid choice for real-time chat systems and snazzy dashboard interfaces.
  • Context: Born from the need for a robust framework that tackles the unique challenges of open-world MMORPGs or the Metaverse, which current open-source libraries just don’t fully address.

Inspiration and Foundation

  • From Web Dev to Game Dev: I shifted from web development (dabbling with frameworks like Spring Boot, ASP.NET to game development, so I drew a lot of inspiration from the web framework galaxy.
  • Main Drive: A burning desire to create a framework with APIs as user-friendly as those in popular web frameworks, but tuned for the high-octane needs of real-time games.

Features of NeoSocket

  • Stateful Connection: Tailored for real-time applications, ensuring continuous connection states for seamless interactions. Saves you from the headache of long-running tasks requiring massive data transfers.
  • Middleware Layer: Allows developers to tweak to their heart’s content. Integrates strong features like DDoS protection and authentication/authorization mechanisms for top-notch user security.
  • Neo-Protocol: A custom-designed protocol that significantly reduces bandwidth usage through optimized features and efficient data handling. The motto here is "Save every bit possible."
  • Bandwidth Efficiency: Uses MessagePack or Protobuf for data standardization. This cuts bandwidth usage by 50-90% compared to JSON. Plus, it saves even more bandwidth with a mechanism that packs multiple packets into a single TCP/UDP packet.
  • Easy Development: Offers a familiar and intuitive structure with Handlers (akin to Controllers), Services, and an integrated IoC (Inversion of Control) container, inspired by common web frameworks.
  • Demand/Unary Request Mechanism: Reflects the Request-Response model common in web development, adapted for high-efficiency network communication.
  • Command/BiDirectional Support: Facilitates command-like requests that don't require responses, enhancing system flexibility and reducing unnecessary data transmission.
  • Packet Management: Uses MessagePack with default configurations, eliminating the need to transmit property names in packets. It employs a unique packet identification system (combining packetNumber and packetName) for efficient data recognition, replacing traditional URL-based methods.

Demo:

Handles up to 400k requests/s on a single-core(2-thread) CPU Google VM (VM also running about 15 services on Docker).

Handles up to 400k requests/s

Server Architecture

Server architecture

Middleware can simple fine-tune by implement interface.

Server architecture

Handles 1000 players in the same scene (simulating a concert in the metaverse)

Metaver Demo 1000 players

One endpoint for all protocols:

[NeoHandler]
public class ChatHandler
{
    [Autowired] private ChatService _chatService { get; set; }
    [HandleMethod]
    public void HandleChat(MessageDTO packet)
    {
        _chatService.HandleChat(packet);
    }
}

Utilizes a single endpoint for TCP/UDP, WebSocket, HTTP, or Unix socket,…

Write and organize code like web but with performance for games, real-time apps.

[Service]
public class ChatService
{
    [Autowired] private IHandleContext _context { get; set; }
    [Autowired] private IChatDB _chatDB { get; set; }
    [Autowired] private ChatManager _chatManager { get; set; }

    public void HandleChat(MessageDTO message)
    {
        //Send message to all user in chatroom
        var chatRoom = _chatManager.GetUser(_context.Sender.State.IdentityId).ChatRoom;
        chatRoom.HandleChat(message);

        //Save message to database
        _chatDB.AddMessage(new MessageEntity
        {
            MessageId = Guid.NewGuid().ToString(),
            RoomName = chatRoom.RoomName,
            UserName = message.UserName,
            Content = message.Content,
            CreatedAt = DateTime.Now
        });
    }
}

Packet-Centric Design:

Everything revolves around packets. Each packet is identified by PacketID and PacketName, serialized using MessagePack to optimize bandwidth. No .protobuf file needed, making the development process smoother.

[NeoPacket]
public class MessageDTO : NeoPacket
{
    [Key(0)] public string UserName { get; set; }

    [Key(1)] public string Content { get; set; }

    [Key(2)] public DateTime CreatedAt { get; set; }
}

Has an SDK for TypeScript, making it deployable in web applications.

@DNeoPacket(0, 'MessageDTO')
export class MessageDTO extends NeoPacket {
  @DKey(0)
  public UserName: string = '';
  @DKey(1)
  public Content: string = '';
  @DKey(2)
  public CreatedAt: Date = new Date();
}

Built-in Container:

Features four scope lifetimes for services like Dotnet, Spring (Scoped, Transient, Singleton), plus a unique Parasitic option (lifetime travels with NeoClient).

[Service]
[Scope(ServiceLifeTime.Parasitic)]
public class ConnectService : IConnectService
{
    [Autowired] private ChatManager _chatManager { get; set; }
    [Autowired] private IHandleContext _context { get; set; }
    [Autowired] private IChatDB _chatDB { get; set; }

Supports [Qualifier] similar to SpringBoot (akin to KeyedService in Dotnet8).

Runs in the background like a Service Worker in Dotnet. It can also be used with services within the Dotnet ecosystem.

Or can run as a Console Application.

Development Progress

  • TCP/UDP Connection: Initially focused on gaming, particularly integration with Unity. The server side was developed using .NET 7 while the client side used .NET Standard 2.0. This configuration achieved the desired performance and efficiency for server-client communication.
  • TypeScript Library Development: A specialized TypeScript SDK was created to support connections from web applications, especially those built with ReactJS. This library provides a bandwidth-saving alternative to traditional HTTP RESTful APIs. However, its stateful architecture makes it less suitable for some types of websites.
  • Development of Monitoring Dashboard: Currently, a ReactJS-based dashboard is under construction. This dashboard aims to provide comprehensive monitoring capabilities for NeoSocket, including server performance measurement and health checks.
  • Metric Integration: Middleware has been written for using Prometheus + Grafana to measure metrics.

Challenges and Prospects

Main Challenges:

  • Performance Optimization: Mastering advanced .NET features to enhance performance, such as data structures, immutable data types, hash tables, caching, parallel programming, context switching, and thread-safe collections.
  • Code Refinement: Facing a lack of uniqueness in the current codebase, primarily using singletons for convenience. Future plans include cleaning up the code and applying best practices for clarity and maintainability.
  • Framework and API Research: Analyzing various frameworks and libraries

to understand which APIs provide the best developer experience. Inspired by IOC Containers and annotations like Autowired and Service in Spring Boot, similar features have been replicated in .NET to enhance developer utility.

Prospects:

  • The Future of NeoSocket: NeoSocket is a high-performance socket framework developed in C#. Potential future applications include developing advanced solutions like message queues and a distributed game server architecture. These applications aim to achieve superior performance with minimal bandwidth waste, leveraging NeoSocket’s efficient design.

Technology Overview

CategoryDetails
EnvironmentsC# .NET 7 (Server side), DotNet Standard 2.0 (Client side), TypeScript
Networking and SocketsSocket Libraries, Stateful Connection
Parallel ProgrammingAsynchronous Programming (Async/Await)
ProtocolsTCP, UDP, WebSocket, HTTP, UnixSocket
Network UtilitiesUPnP, UDP Hole Punching
Serialization/DeserializationMessagePack (for efficient data handling)
Scan and CachingReflection, Attribute
Operation Systems SupportedWindows, MacOS, Linux
Currently Supported PlatformsUnity, ReactJS, JavaScript/TypeScript, DotNet application