# anephenix/sarus architecture

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

- Interactive diagram: https://gitdiagram.com/anephenix/sarus
- Repository: https://github.com/anephenix/sarus

## Overview

##### Main Components Identification

a. Core WebSocket Management Layer
- WebSocket Connection Handler
- Automatic Reconnection System
- Event Listener Manager
- Message Queue System

b. Storage Systems
- In-Memory Storage
- Browser Storage Integration (LocalStorage/SessionStorage)

c. Configuration Management
- Connection Options
- Exponential Backoff Handler
- Data Transformers

**[Client Application] → [Sarus Library]**
- ↓
- ┌─────────────────────────────────────────┐
- Sarus Core                 │
- ┌─────────────┐      ┌──────────────┐ │
- WebSocket  │←────→│Event Listener│ │
- Manager    │      │   Manager    │ │
- ┘      └──────────────┘ │
- ↕                     ↕        │
- ┌─────────────┐      ┌──────────────┐ │
- Message    │←────→│   Storage    │ │
- Queue     │      │   Bridge     │ │
- ┘      └──────────────┘ │
- ┘
- ↕
- ┌─────────────────────────────────────────┐
- Storage Implementations         │
- ┌─────────────┐      ┌──────────────┐ │
- In-Memory  │      │  Browser     │ │
- Storage   │      │   Storage    │ │
- ┘      └──────────────┘ │
- ┘
- ↓
- [WebSocket Server Connection]

##### Component Details

For each major component, include:

a. WebSocket Manager:
- Connection state handling
- Reconnection logic with exponential backoff
- Protocol management

b. Event Listener Manager:
- Event binding/unbinding
- Event persistence across reconnections
- Event type handling (open, message, close, error)

c. Message Queue:
- Message persistence
- Retry logic
- Queue state management

d. Storage Bridge:
- Storage type selection (memory/session/local)
- Storage quota management
- Data serialization/deserialization

##### Important Notes

Specific callouts:
- Automatic reconnection flow
- Message queue persistence options
- Exponential backoff logic
- Event listener rebinding process

##### Implementation Details

Technical notes showing:
- TypeScript implementation
- Browser compatibility
- Storage quotas and limitations
- Protocol support

##### Integration Points

Clearly mark:
- Client application integration points
- WebSocket server connection details
- Storage system interfaces
- Event handling hooks

This design accurately represents Sarus's architecture while highlighting its robust handling of WebSocket connections, event management, and message queuing capabilities.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        CA[Client Application]
    end

    subgraph SarusCore["Sarus Core Library"]
        WM["WebSocket Manager"]:::core
        click WM "https://github.com/anephenix/sarus/blob/master/src/index.ts"
        
        ELM["Event Listener Manager"]:::core
        click ELM "https://github.com/anephenix/sarus/blob/master/src/index.ts"
        
        MQ["Message Queue System"]:::core
        click MQ "https://github.com/anephenix/sarus/blob/master/src/index.ts"
        
        DT["Data Transformer"]:::core
        click DT "https://github.com/anephenix/sarus/blob/master/src/lib/dataTransformer.ts"
        
        CO["Connection Options"]:::config
        click CO "https://github.com/anephenix/sarus/blob/master/src/index.ts"
        
        VAL["Input/Output Validator"]:::core
        click VAL "https://github.com/anephenix/sarus/blob/master/src/lib/validators.ts"
        
        CONST["Constants Manager"]:::config
        click CONST "https://github.com/anephenix/sarus/blob/master/src/lib/constants.ts"
        
        RS["Reconnection System"]:::core
        click RS "https://github.com/anephenix/sarus/blob/master/src/index.ts"
        
        SM["State Manager"]:::core
        click SM "https://github.com/anephenix/sarus/blob/master/src/index.ts"
        
        CRL["Connection Retry Logic"]:::core
        click CRL "https://github.com/anephenix/sarus/blob/master/src/index.ts"
    end

    subgraph Storage["Storage Layer"]
        IMS["In-Memory Storage"]:::storage
        BS["Browser Storage"]:::storage
    end

    subgraph Server
        WS["WebSocket Server"]
    end

    %% Connections
    CA --> WM
    WM <--> ELM
    WM <--> MQ
    WM <--> RS
    RS --> CRL
    CRL --> SM
    MQ <--> Storage
    WM <--> DT
    DT <--> VAL
    CO --> WM
    CONST --> WM
    WM --> WS

    %% Styles
    classDef core fill:#2374ab,stroke:#000,stroke-width:2px,color:#fff
    classDef storage fill:#57a773,stroke:#000,stroke-width:2px,color:#fff
    classDef config fill:#ffc857,stroke:#000,stroke-width:2px,color:#000
    
    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Storage Systems"]:::storage
        L3["Configuration"]:::config
    end
```
