# timetocode/nengi architecture

> Architecture diagram of the GitHub repository timetocode/nengi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/timetocode/nengi
- Repository: https://github.com/timetocode/nengi

## Overview

##### Major Architectural Components
- Core Library (core): This is the backbone of nengi.js. It contains most of the functionality divided into distinct areas:
– Binary Module (core/binary): Handles binary serialization/deserialization, with various types (e.g. Int8, Float32, ASCIIString, etc.) and helper utilities (e.g., BitBuffer, BitStream). This module is responsible for preparing game state snapshots and network messages for efficient transport.
– Protocol Module (core/protocol): Manages the rules and schemas used for transmitting commands, messages, entities, and other data, including helper functions for reading/writing and type selections.
– Snapshot Module (core/snapshot): Responsible for creating, reading, and writing snapshots that capture the game state. It breaks down the reading and writing of different entity operations (create, update, delete) into submodules.
– Client Module (core/client): Contains the machinery for client-side operations such as prediction, interpolation, and the management of world state (e.g., Chronus, Predictor, Interpolator). This is where the HTML5 client consumes and processes the networked updates.
– Instance Module (core/instance): Manages the server’s runtime state including entities, channels, caches, and state histories needed for authoritative synchronization and rollback features (historian, spatial channels, etc).
– Bot Module (core/bot): Supports AI or simulated clients (bots) that interact with the network engine.
– Common and Defaults (core/common and core/defaults.js): Contain shared utilities and default settings that span across the library.
- External Integration: External single files (external/EDictionary.js) and the index files (index.js, index-es6.js, etc.) that expose the library's API for both node.js and browser environments.
- Testing Framework (spec): A suite of spec/test files that target different modules (bot, channel, proxy, etc.), ensuring that the components work as expected.

##### Relationships and Interactions
- Data Flow:
– On the server, an nengi.Instance manages game entities. Changes to entities (via the Entity stack) are routed through the Instance module.
– The Protocol module translates these entity updates (create, update, delete) into binary messages using the Binary module.
– The Snapshot module then constructs snapshots of the current state, packaging them for transmission.
– Over on the client side (nengi.Client and associated client modules), incoming snapshots are processed. Interpolator and Predictor tools smooth out movement and state updates.
– Optional bot components feed simulated inputs through the system for testing or automated gameplay.
- Dependency Flow:
– The Protocol, Snapshot, and Binary modules are closely interrelated, as they work sequentially: the Protocol defines schema → Binary encodes/decodes these schemas → Snapshot builds or reads data packets.
– The Instance module integrates with both client and server submodules to provide a central source of truth.

##### Architectural Patterns and Principles
- Modular Design: The clear separation between binary handling, protocol definitions, snapshot management, and client logic.
- Separation of Concerns: Distinct modules for server runtime (instance, protocol, snapshot) versus client-side predictive and interpolation support.
- Performance and Efficiency: Emphasis on binary compression along with real-time synchronization techniques for responsiveness and volume scalability.

##### Relevant Technologies and Frameworks
- Node.js as the runtime for the server.
- HTML5 / WebSocket-based communication, with potential usage of libraries like cWS.js or uWS.js (as mentioned in the README).
- Industry-standard game development libraries (e.g., PIXI.js, Babylon.js, Three.js) are supported by the client.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Server Side Components
    subgraph "Server Side"
        A["Instance Module"]:::stateManagement
        B["Protocol Module"]:::serialization
        C["Binary Module"]:::serialization
        D["Snapshot Module"]:::serialization
        E["Bot Module"]:::auxiliary
        F["Common & Defaults"]:::auxiliary
        G["Defaults File"]:::auxiliary

        A -->|"entityUpdates"| B
        B -->|"serializeSchema"| C
        C -->|"preparePackets"| D
        E -->|"simulateInputs"| A
        F --- G
    end

    %% Client Side Components
    subgraph "Client Side"
        H["Client Module"]:::clientProcessing
        I["Interpolator"]:::clientProcessing
        J["Predictor"]:::clientProcessing
        K["WorldState"]:::clientProcessing
        L["Chronus"]:::clientProcessing

        D -->|"snapshotData"| H
        H --> I
        I --> J
        J --> K
        K --> L
    end

    %% External and Testing
    subgraph "External & Integration"
        M["External Integration"]:::external
        M -->|"exposesAPI"| H
    end

    subgraph "Testing"
        N["Testing Framework"]:::testing
        N --- A
        N --- B
        N --- C
        N --- D
        N --- H
    end

    %% Data dependency between Protocol and Binary modules
    B ---|"dependsOn"| C

    %% Click Events
    click C "https://github.com/timetocode/nengi/tree/master/core/binary"
    click B "https://github.com/timetocode/nengi/tree/master/core/protocol"
    click D "https://github.com/timetocode/nengi/tree/master/core/snapshot"
    click H "https://github.com/timetocode/nengi/blob/master/core/client/Client.js"
    click A "https://github.com/timetocode/nengi/tree/master/core/instance"
    click E "https://github.com/timetocode/nengi/tree/master/core/bot"
    click F "https://github.com/timetocode/nengi/tree/master/core/common"
    click G "https://github.com/timetocode/nengi/blob/master/core/defaults.js"
    click M "https://github.com/timetocode/nengi/blob/master/external/EDictionary.js"
    click N "https://github.com/timetocode/nengi/tree/master/spec"

    %% Styles
    classDef serialization fill:#f9e79f,stroke:#b7950b,stroke-width:2px;
    classDef stateManagement fill:#aed6f1,stroke:#2471a3,stroke-width:2px;
    classDef clientProcessing fill:#a9dfbf,stroke:#196f3d,stroke-width:2px;
    classDef auxiliary fill:#f5b7b1,stroke:#943126,stroke-width:2px;
    classDef external fill:#fad7a0,stroke:#d35400,stroke-width:2px;
    classDef testing fill:#d7bde2,stroke:#6c3483,stroke-width:2px;
```
