# deepstreamio/deepstream.io-client-js architecture

> Architecture diagram of the GitHub repository deepstreamio/deepstream.io-client-js, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/deepstreamio/deepstream.io-client-js
- Repository: https://github.com/deepstreamio/deepstream.io-client-js

## Overview

##### Main Components

a) Core Client Layer:
- Main `DeepstreamClient` class (from deepstream-client.ts)
- This serves as the primary entry point for all client operations

b) Connection Layer:
- Connection management system (from /connection directory)
- WebSocket factory and connection handling

c) Feature Modules (represent as separate components):
- Records Module (data sync)
- Events Module (pub/sub)
- RPC Module (remote procedure calls)
- Presence Module (user presence tracking)

d) Storage Layer:
- Three storage implementations:
- IndexDB Storage
- LocalStorage
- NoOp Storage

e) Utility Services:
- Timeout Registry
- Interval Timer Registry
- Bulk Subscription Service
- State Machine
- Logger

##### Relationships and Data Flow

**Client Layer**
- ↓↑
**Connection Layer**
- ↓↑
**Feature Modules → Storage Layer**
- ↓↑
**Utility Services**

##### Specific Design Elements

- Rectangles: Core components (Client, Connection)
- Hexagons: Feature modules
- Rounded rectangles: Utility services

- Blue: Core client and connection components
- Green: Feature modules
- Yellow: Storage layer
- Gray: Utility services

c) Connection types:
- Double lines: Data synchronization paths

##### Technical Details to Highlight

- TypeScript implementation (show as a label)
- Browser and Node.js compatibility
- React Native support
- WebSocket communication
- State management capabilities

##### Implementation Notes

Create three layers of abstraction:

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        DC["DeepstreamClient"]:::core
        click DC "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/deepstream-client.ts"
    end

    subgraph Connection
        CH["Connection Handler"]:::connection
        SF["Socket Factory"]:::connection
        click CH "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/connection/connection.ts"
        click SF "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/connection/socket-factory.ts"
    end

    subgraph "Feature Modules"
        RM["Records Module"]:::feature
        EM["Events Module"]:::feature
        RPCM["RPC Module"]:::feature
        PM["Presence Module"]:::feature
        click RM "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/record/record-handler.ts"
        click EM "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/event/event-handler.ts"
        click RPCM "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/rpc/rpc-handler.ts"
        click PM "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/presence/presence-handler.ts"
    end

    subgraph Storage
        IDB["IndexDB Storage"]:::storage
        LS["LocalStorage"]:::storage
        NS["NoOp Storage"]:::storage
        click IDB "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/storage/indexdb-storage-service.ts"
        click LS "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/storage/localstorage-storage-service.ts"
        click NS "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/storage/noop-storage-service.ts"
    end

    subgraph "Utility Services"
        TR["Timeout Registry"]:::utility
        ITR["Interval Timer Registry"]:::utility
        BSS["Bulk Subscription Service"]:::utility
        SM["State Machine"]:::utility
        LOG["Logger"]:::utility
        click TR "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/util/timeout-registry.ts"
        click ITR "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/util/interval-timer-registry.ts"
        click BSS "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/util/bulk-subscription-service.ts"
        click SM "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/util/state-machine.ts"
        click LOG "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/util/logger.ts"
    end

    subgraph "Specialized Components"
        JPH["JSON Path Handler"]:::specialized
        MSS["Merge Strategy Service"]:::specialized
        WAS["Write Acknowledgment Service"]:::specialized
        SN["Single Notifier"]:::specialized
        click JPH "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/record/json-path.ts"
        click MSS "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/record/merge-strategy-service.ts"
        click WAS "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/record/write-ack-service.ts"
        click SN "https://github.com/deepstreamIO/deepstream.io-client-js/blob/master/src/record/single-notifier.ts"
    end

    %% Connections
    DC --> CH
    CH --> SF
    DC --> RM & EM & RPCM & PM
    RM & EM & RPCM & PM --> IDB & LS & NS
    RM --> JPH & MSS & WAS & SN
    DC -.-> TR & ITR & BSS & SM & LOG

    %% Styles
    classDef core fill:#2374ab,stroke:#000,stroke-width:2px,color:white
    classDef connection fill:#4a90e2,stroke:#000,stroke-width:2px,color:white
    classDef feature fill:#47b881,stroke:#000,stroke-width:2px,color:white
    classDef storage fill:#ffd700,stroke:#000,stroke-width:2px,color:black
    classDef utility fill:#95a5a6,stroke:#000,stroke-width:2px,color:white
    classDef specialized fill:#8e44ad,stroke:#000,stroke-width:2px,color:white

    %% Legend
    subgraph Legend
        L1["Core Component"]:::core
        L2["Connection Component"]:::connection
        L3["Feature Module"]:::feature
        L4["Storage Component"]:::storage
        L5["Utility Service"]:::utility
        L6["Specialized Component"]:::specialized
    end
```
