# undefinedofficial/u-connect-client-ts architecture

> Architecture diagram of the GitHub repository undefinedofficial/u-connect-client-ts, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/undefinedofficial/u-connect-client-ts
- Repository: https://github.com/undefinedofficial/u-connect-client-ts

## Overview

##### Project Type and Purpose
- This is a client SDK library (u-connect/client-ts) for performing RPC over WebSockets using MessagePack.
- It exposes a UConnectClient facade to your app, supports four RPC modes (Unary, ServerStream, ClientStream, DuplexStream), and is shipped as an npm package.

##### File Structure
Top-level:
- config & tooling: .eslintrc.cjs, .prettierrc.json, tsconfig*.json, vite.config.ts
- build output: dist/ (contains compiled JS and type declarations)
- example/: a Vue 3 sample app consuming the library
- u-connect/: core TypeScript modules
Inside u-connect/:
- Interfaces: IConnection.ts, ILogger.ts, ISerializer.ts, IUConnectClient.ts, IUniqueIdProvider.ts
- Core classes: ClientStream.ts, ServerStream.ts, Emitter.ts, PromiceValue.ts, Service.ts, UConnectClient (index.ts)
- Supporting modules: DataType.ts, Exceptions.ts, Status.ts
Others:
- env.d.ts, example shims, package.json and lock files

##### Extract Key Architecture Insights from README
- The “UConnectClient” is the main entrypoint—composes a Connection + Logger + Serializer.
- RPC modes map to four stream types, each with a well-defined interface.
- Default transport is WebSocket, default serialization is MessagePack.
- The example demonstrates integration in a Vue SPA.

##### Main Components
a. Client Application
b. UConnectClient Facade
c. Connection Layer (IConnection implementation, e.g. WebSocketConnection)
d. Serializer Layer (MessagePack via ISerializer)
e. Logger (ConsoleLogger implementing ILogger)
f. Service Facade (Service.ts, exposes .unary(), .serverStream(), etc.)
g. Stream Handlers:
– UnaryRequest / UnaryResponse
– ServerStream
– ClientStream
– DuplexStream
h. Internal Utilities:
– Emitter (event bus)
– PromiceValue (promise wrapper)
– UniqueIdProvider (message IDs)
i. External Service: u-connect Server (not in repo, shown as external)

##### Relationships & Data Flow
- The App invokes UConnectClient.service("ServiceName") → returns a Service proxy.
- Service methods (unary/serverStream/…) call into Service.ts, which:
1) Generates a unique request ID (IUniqueIdProvider)
2) Serializes method name, params via ISerializer
3) Dispatches payload to IConnection.send()
4) Uses Emitter to route incoming messages back to the correct Stream handler or Promise
- IConnection maintains an open WebSocket, handles reconnect logic
- Streams push data to the App via callbacks (.onMessage, .onError, .onEnd)

##### Architectural Patterns & Principles
- Facade: UConnectClient & Service.ts hide complex stream logic
- Adapter: IConnection allows swapping WebSocket implementations
- Strategy: ISerializer lets you choose MessagePack or a custom serializer
- Observer/Event-Emitter: internal Emitter dispatches messages to handlers
- Promise-based & event-driven concurrency

##### Technologies & Frameworks
- TypeScript (source in u-connect/, builds to dist/)
- WebSocket API (browser or ws in Node.js)
- MessagePack binary protocol (default serializer)
- Example Vue 3 SPA

## Diagram (Mermaid)

```mermaid
flowchart TB
  %% Application Layer
  subgraph "Application Layer"
    ExampleApp["Vue 3 App"]:::frontend
  end

  %% SDK Entry & Facades
  subgraph "SDK: Entry & Facades"
    UConnectClient["UConnectClient"]:::sdk
    Service["Service (RPC Methods)"]:::sdk
  end

  %% SDK Streams
  subgraph "SDK: Stream Handlers"
    ClientStream["ClientStream (Unary)"]:::sdk
    ServerStream["ServerStream"]:::sdk
    DuplexStream["DuplexStream"]:::sdk
    ClientStream -->|"handles Unary"| Service
    ServerStream -->|"handles ServerStream"| Service
    DuplexStream -->|"handles DuplexStream"| Service
  end

  %% SDK Interfaces & Utilities
  subgraph "SDK: Interfaces & Utilities"
    IUConnectClient["IUConnectClient"]:::util
    IConnection["IConnection"]:::util
    ISerializer["ISerializer"]:::util
    ILogger["ILogger"]:::util
    IUniqueIdProvider["IUniqueIdProvider"]:::util
    PromiceValue["PromiceValue"]:::util
    Emitter["Emitter"]:::util
    DataType["DataType"]:::util
    Exceptions["Exceptions"]:::util
    Status["Status"]:::util
  end

  %% Transport & External
  subgraph "Transport & External"
    WebSocket["WebSocketTransport"]:::transport
    ExternalServer["u-connect Server"]:::external
  end

  %% Build Output
  subgraph "Build Output & Example"
    Dist["dist/"]:::docs
    ExampleDir["example/"]:::frontend
  end

  %% Connections
  ExampleApp -->|"uses"| UConnectClient
  UConnectClient -->|"creates Service"| Service
  Service -->|"generates IDs"| IUniqueIdProvider
  Service -->|"serializes via"| ISerializer
  Service -->|"logs via"| ILogger
  Service -->|"wraps Promises"| PromiceValue
  Service -->|"emits events via"| Emitter
  Service -->|"sends over"| IConnection

  IConnection -->|"uses"| WebSocket
  WebSocket -->|"frames"| ExternalServer
  ExternalServer -->|"frames"| WebSocket
  WebSocket -->|"messages"| IConnection
  IConnection -->|"onMessage"| Emitter

  UConnectClient -->|"exposes Interface"| IUConnectClient

  %% Example and build relations
  ExampleApp -->|"example code in"| ExampleDir
  UConnectClient -->|"built to"| Dist

  %% Click Events
  click UConnectClient "https://github.com/undefinedofficial/u-connect-client-ts/blob/main/u-connect/index.ts"
  click Service "https://github.com/undefinedofficial/u-connect-client-ts/blob/main/u-connect/Service.ts"
  click ClientStream "https://github.com/undefinedofficial/u-connect-client-ts/blob/main/u-connect/ClientStream.ts"
  click ServerStream "https://github.com/undefinedofficial/u-connect-client-ts/blob/main/u-connect/ServerStream.ts"
  click Emitter "https://github.com/undefinedofficial/u-connect-client-ts/blob/main/u-connect/Emitter.ts"
  click PromiceValue "https://github.com/undefinedofficial/u-connect-client-ts/blob/main/u-connect/PromiceValue.ts"
  click IUniqueIdProvider "https://github.com/undefinedofficial/u-connect-client-ts/blob/main/u-connect/IUniqueIdProvider.ts"
  click IConnection "https://github.com/undefinedofficial/u-connect-client-ts/blob/main/u-connect/IConnection.ts"
  click ISerializer "https://github.com/undefinedofficial/u-connect-client-ts/blob/main/u-connect/ISerializer.ts"
  click ILogger "https://github.com/undefinedofficial/u-connect-client-ts/blob/main/u-connect/ILogger.ts"
  click IUConnectClient "https://github.com/undefinedofficial/u-connect-client-ts/blob/main/u-connect/IUConnectClient.ts"
  click DataType "https://github.com/undefinedofficial/u-connect-client-ts/blob/main/u-connect/DataType.ts"
  click Exceptions "https://github.com/undefinedofficial/u-connect-client-ts/blob/main/u-connect/Exceptions.ts"
  click Status "https://github.com/undefinedofficial/u-connect-client-ts/blob/main/u-connect/Status.ts"
  click ExampleDir "https://github.com/undefinedofficial/u-connect-client-ts/tree/main/example/"
  click Dist "https://github.com/undefinedofficial/u-connect-client-ts/tree/main/dist/"

  %% Styles
  classDef frontend fill:#E3F2FD,stroke:#90CAF9,color:#0D47A1
  classDef sdk fill:#E8F5E9,stroke:#66BB6A,color:#1B5E20
  classDef transport fill:#FFF3E0,stroke:#FFB74D,color:#E65100
  classDef external fill:#FFE0B2,stroke:#FF9800,color:#BF360C
  classDef util fill:#ECEFF1,stroke:#90A4AE,color:#37474F
  classDef docs fill:#F3E5F5,stroke:#CE93D8,color:#4A148C
```
