# clockworklabs/spacetimedb-typescript-sdk architecture

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

- Interactive diagram: https://gitdiagram.com/clockworklabs/spacetimedb-typescript-sdk
- Repository: https://github.com/clockworklabs/spacetimedb-typescript-sdk

## Overview

##### Project Type and Purpose
- The README tells us this is a TypeScript SDK for SpacetimeDB. Its aim is to provide clear APIs, event handling, connection management, and data serialization for clients wishing to interact with SpacetimeDB.
- It is built as a monorepo using pnpm (as seen by pnpm-lock.yaml and pnpm-workspace.yaml) with multiple packages, indicating the use of modular design.

##### File Structure
- Top-level directories include configuration-related files (.github, .vscode, etc.) and crucial documentation (README.md, DEVELOP.md).
- The “packages” directory is particularly important:
- The “packages/sdk” folder houses the entire SDK. Internally, it is structured with a "src" subdirectory that contains modules such as client_api (with many API type definitions for client, server messaging, subscription handling, etc.), connection utilities, event/emitter implementations, and serializers.
- The “packages/test-app” folder serves as a sample or demo application that uses the SDK. It contains both a frontend (with React components like App.tsx, main.tsx, etc.) and a backend (a Rust server under packages/test-app/server).
- The “examples” directory (e.g., quickstart-chat) provides another usage example, hinting at how developers should integrate or quickly spin up a chat app using the SDK.

##### Key Components
a. The Core SDK:
- Client API Module: Contains multiple types including subscription types, query types, and reducers that form the client-server communication contract.
- Connection & Communication: Components like db_connection_builder, websocket adapters (e.g., websocket_decompress_adapter.ts, websocket_test_adapter.ts) handle establishing connections and processing messages.
- Utilities & Helpers: Modules for serialization (serializer.ts), event handling (event_emitter.ts, event.ts, event_context.ts), and logging (logger.ts).
- Data Models: Modules like algebraic_type, algebraic_value, and message_types that define the data structures.

b. Complementary and Example Applications:
- Test App: A sample client to demonstrate SDK usage with its own frontend (React-based UI components) and a server component in Rust, indicating cross-technology integration.
- Quickstart Example: An additional, simpler setup (a chat example) to help new users see how to quickly integrate/use the SDK.

##### Relationships and Interactions
- The SDK acts as the central library that provides the API and connection management to connect client applications (like the test app and quickstart example) to SpacetimeDB.
- Client applications (both in packages/test-app and examples) depend on the SDK to initialize connections, send/receive messages, subscribe to data updates, and manage events.
- The internal modules within the SDK interact in layered fashion: lower-level utilities (serializer, binary read/write) support the client API methods, which in turn interface with connection builders and network adapters.

##### Architectural Patterns and Design Principles
- Modular Design: The code is organized into specialized directories (client_api, utilities, adapters) that adhere to separation of concerns.
- Event-Driven Communication: With modules like event_emitter and reducer_event, the design suggests an approach focused on event handling.
- Clear Type and Interface Definitions: Strong use of TypeScript’s typing system ensures that message types and database interactions are consistent.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Service Layer
    subgraph "External Service"
        ST["SpacetimeDB Service"]:::external
    end

    %% Core SDK Library Layer
    subgraph "Core SDK Library"
        direction TB
        API["Client API Module\n(interfaces for client-server messaging)"]:::sdk

        subgraph "Connection & Communication"
            direction TB
            DBB["DB Connection Builder"]:::conn
            DBI["DB Connection Implementation"]:::conn
            WSD["WebSocket Decompress Adapter"]:::conn
            WST["WebSocket Test Adapter"]:::conn
        end

        subgraph "Serializer & Utilities"
            direction TB
            SER["Serializer\n(handling message conversion)"]:::util
            UTIL["Utilities"]:::util
        end

        subgraph "Event Handling"
            direction TB
            EEM["Event Emitter"]:::event
            EVT["Event System"]:::event
            ECX["Event Context"]:::event
            RED["Reducer Event"]:::event
        end

        subgraph "Data Models"
            direction TB
            AT["Algebraic Type"]:::model
            AV["Algebraic Value"]:::model
            MT["Message Types"]:::model
        end
    end

    %% Applications Layer
    subgraph "Applications"
        direction TB
        TA["Test App\n(Frontend & Backend)"]:::app
        QC["Quickstart Chat Example"]:::app
    end

    %% Data Flow/Relationships
    TA -->|"calls"| API
    QC -->|"uses"| API

    API -->|"initiates"| DBB
    API -->|"utilizes"| SER
    API -->|"triggers"| EEM

    DBB -->|"connectsTo"| ST
    DBI -->|"connectsTo"| ST
    WSD -->|"adaptsFor"| ST
    WST -->|"tests"| ST

    SER -->|"supports"| API
    UTIL -->|"supports"| API

    EEM -->|"emitsTo"| EVT
    EVT -->|"manages"| ECX
    ECX -->|"updatesVia"| RED

    AT -->|"defines"| MT
    AV -->|"defines"| MT

    %% Click Events
    click API "https://github.com/clockworklabs/spacetimedb-typescript-sdk/tree/main/packages/sdk/src/client_api"
    click DBB "https://github.com/clockworklabs/spacetimedb-typescript-sdk/blob/main/packages/sdk/src/db_connection_builder.ts"
    click DBI "https://github.com/clockworklabs/spacetimedb-typescript-sdk/blob/main/packages/sdk/src/db_connection_impl.ts"
    click WSD "https://github.com/clockworklabs/spacetimedb-typescript-sdk/blob/main/packages/sdk/src/websocket_decompress_adapter.ts"
    click WST "https://github.com/clockworklabs/spacetimedb-typescript-sdk/blob/main/packages/sdk/src/websocket_test_adapter.ts"
    click SER "https://github.com/clockworklabs/spacetimedb-typescript-sdk/blob/main/packages/sdk/src/serializer.ts"
    click UTIL "https://github.com/clockworklabs/spacetimedb-typescript-sdk/blob/main/packages/sdk/src/utils.ts"
    click EEM "https://github.com/clockworklabs/spacetimedb-typescript-sdk/blob/main/packages/sdk/src/event_emitter.ts"
    click EVT "https://github.com/clockworklabs/spacetimedb-typescript-sdk/blob/main/packages/sdk/src/event.ts"
    click ECX "https://github.com/clockworklabs/spacetimedb-typescript-sdk/blob/main/packages/sdk/src/event_context.ts"
    click RED "https://github.com/clockworklabs/spacetimedb-typescript-sdk/blob/main/packages/sdk/src/reducer_event.ts"
    click AT "https://github.com/clockworklabs/spacetimedb-typescript-sdk/blob/main/packages/sdk/src/algebraic_type.ts"
    click AV "https://github.com/clockworklabs/spacetimedb-typescript-sdk/blob/main/packages/sdk/src/algebraic_value.ts"
    click MT "https://github.com/clockworklabs/spacetimedb-typescript-sdk/blob/main/packages/sdk/src/message_types.ts"
    click TA "https://github.com/clockworklabs/spacetimedb-typescript-sdk/tree/main/packages/test-app"
    click QC "https://github.com/clockworklabs/spacetimedb-typescript-sdk/tree/main/examples/quickstart-chat"

    %% Style Definitions
    classDef sdk fill:#f9f,stroke:#333,stroke-width:2px;
    classDef conn fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef util fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef event fill:#ccccff,stroke:#333,stroke-width:2px;
    classDef model fill:#ffffcc,stroke:#333,stroke-width:2px;
    classDef app fill:#d9edf7,stroke:#31708f,stroke-width:2px;
    classDef external fill:#ffd699,stroke:#ff9900,stroke-width:2px;
```
