# oeway/hypha-rpc architecture

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

- Interactive diagram: https://gitdiagram.com/oeway/hypha-rpc
- Repository: https://github.com/oeway/hypha-rpc

## Overview

##### Main Components
- Client Libraries (Python and JavaScript): Represent separate modules that handle the RPC communication. Each has its own “src” or “hypha_rpc” code base with utilities for transport management, encoding/decoding data types, and handling remote calls.
- Transport Layer: Identify the business logic that manages two different transports:
– WebSocket Clients (e.g., javascript/src/websocket-client.js and python/hypha_rpc/websocket_client.py)
– WebRTC support (e.g., javascript/src/webrtc-client.js and python/hypha_rpc/pyodide_sse.py, along with rtc service functions)
- Data Encoding/Decoding Mechanism: Components responsible for custom codecs, type conversion, and unified data representation. Illustrate the codec registration and data transformation layer.
- RPC Core Logic: The central module that integrates function calls (both synchronous and asynchronous), including how remote calls traverse the transport layer.

##### Relationships and Interactions
- Flow where custom data objects are first encoded (using custom codecs) on the client, sent as JSON (with potential binary data), and then decoded on the other end.
- Interactions such as login and connection establishment that involve token generation, plus the optional registration of telemetry or login callbacks.

##### Architectural Patterns and Design Principles
- Modular Design: Emphasize a separation between language-specific client implementations with common shared concepts (RPC invocation, codec handling).
- Client-Server Model: Clearly demarcate the client libraries (consider grouping the JavaScript and Python implementations side by side) and the external Hypha server that provides services.
- Layered Architecture: At the lowest level, the transport layer (WebSocket, WebRTC). In the middle, the encoding/decoding logic, and at the top, the RPC interfaces offered to the developer (including synchronous wrappers).
- Extensibility: Highlight the codec registration pattern which allows support for custom data types.

##### Technologies, Frameworks, and Libraries
- In JavaScript: ESLint, Prettier for style; Webpack for bundling; and specialized client modules (rpc.js, websocket-client.js, webrtc-client.js).
- In Python: Packaging files (setup.cfg, pyproject.toml), asynchronous modules (asyncio), and the synchronous wrapper (hypha_rpc.sync).
- Both sides use JSON for structured data representation and are set up to handle binary as well via ArrayBuffer/bytes.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Python Client Library Subgraph
    subgraph "Python Client Library"
        P1["RPC Core (rpc.py)"]:::client
        P2["Sync API (sync.py)"]:::client
        P3["Python Codec Module"]:::codec
    end

    %% Python Transport Layer Subgraph
    subgraph "Python Transport Layer"
        WS1["WebSocket Client"]:::transport
        WS2["Pyodide WebSocket Support"]:::transport
        subgraph "Python WebRTC Transport"
            RTC1["Pyodide SSE"]:::transport
            RTC2["WebRTC Client"]:::transport
        end
    end

    %% JavaScript Client Library Subgraph
    subgraph "JavaScript Client Library"
        J1["RPC Core (rpc.js)"]:::client
        J2["JavaScript Codec Module"]:::codec
    end

    %% JavaScript Transport Layer Subgraph
    subgraph "JavaScript Transport Layer"
        J_WS["WebSocket Client"]:::transport
        J_RTC["WebRTC Client"]:::transport
    end

    %% External Hypha Server
    HS["Hypha Server"]:::server

    %% Build/Dev & CI Infrastructure Subgraph
    subgraph "Build/Dev & CI Infrastructure"
        BD_CI["CI Workflows"]:::infra
        BD_Python["Python Packaging & Config"]:::infra
        BD_JS["JS Package Config"]:::infra
    end

    %% Python Client to Transport Layer Connections
    P1 -->|"RPC_call"| WS1
    P1 -->|"RPC_call"| RTC1
    P2 -->|"RPC_call"| WS1
    P2 -->|"RPC_call"| RTC1
    P1 -- "uses" --> P3
    P2 -- "uses" --> P3

    %% JavaScript Client to Transport Layer Connections
    J1 -->|"RPC_call"| J_WS
    J1 -->|"RPC_call"| J_RTC
    J1 -- "uses" --> J2

    %% Transport Layer to Hypha Server Connections (Python)
    WS1 -->|"transports"| HS
    WS2 -->|"support"| HS
    RTC1 -->|"transports"| HS
    RTC2 -->|"transports"| HS

    %% Transport Layer to Hypha Server Connections (JavaScript)
    J_WS -->|"transports"| HS
    J_RTC -->|"transports"| HS

    %% Hypha Server Response back to Clients
    HS -->|"response"| P1
    HS -->|"response"| J1

    %% Click Events for Python Client Library
    click P1 "https://github.com/oeway/hypha-rpc/blob/main/python/hypha_rpc/rpc.py"
    click P2 "https://github.com/oeway/hypha-rpc/blob/main/python/hypha_rpc/sync.py"
    click P3 "https://github.com/oeway/hypha-rpc/blob/main/python/hypha_rpc/utils/schema.py"

    %% Click Events for Python Transport Layer
    click WS1 "https://github.com/oeway/hypha-rpc/blob/main/python/hypha_rpc/websocket_client.py"
    click WS2 "https://github.com/oeway/hypha-rpc/blob/main/python/pyodide_websocket.py"
    click RTC1 "https://github.com/oeway/hypha-rpc/blob/main/python/hypha_rpc/pyodide_sse.py"
    click RTC2 "https://github.com/oeway/hypha-rpc/blob/main/python/hypha_rpc/webrtc_client.py"

    %% Click Events for JavaScript Client Library and Transport
    click J1 "https://github.com/oeway/hypha-rpc/blob/main/javascript/src/rpc.js"
    click J2 "https://github.com/oeway/hypha-rpc/blob/main/javascript/src/utils/schema.js"
    click J_WS "https://github.com/oeway/hypha-rpc/blob/main/javascript/src/websocket-client.js"
    click J_RTC "https://github.com/oeway/hypha-rpc/blob/main/javascript/src/webrtc-client.js"

    %% Click Events for Build/Dev & CI Infrastructure
    click BD_CI "https://github.com/oeway/hypha-rpc/tree/main/.github/workflows/"
    click BD_Python "https://github.com/oeway/hypha-rpc/blob/main/python/setup.cfg"
    click BD_JS "https://github.com/oeway/hypha-rpc/blob/main/javascript/package.json"

    %% Styles
    classDef client fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef transport fill:#A9DFBF,stroke:#196F3D,stroke-width:2px;
    classDef codec fill:#FCF3CF,stroke:#B7950B,stroke-width:2px;
    classDef server fill:#F5B7B1,stroke:#943126,stroke-width:2px;
    classDef infra fill:#D5DBDB,stroke:#707B7C,stroke-width:2px;
```
