# jmmoser/lifxlan architecture

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

- Interactive diagram: https://gitdiagram.com/jmmoser/lifxlan
- Repository: https://github.com/jmmoser/lifxlan

## Overview

##### Main Components

a) Core Components (Central Layer):
- Client Module (client.js)
- Router Module (router.js)
- Devices Manager (devices.js)
- Groups Manager (groups.js)

b) Supporting Components (Lower Layer):
- Commands Handler (commands.js)
- Encoding Module (encoding.js)
- Utils (utils.js)
- Constants (constants.js)

##### External Integration Points
- UDP Socket Interface (show as an external dependency)
- LIFX Devices (physical endpoints)

##### Component Relationships
- Client connecting to Router (command routing)
- Devices Manager to Router (device tracking)
- Link Groups Manager to Devices Manager (group organization)
- Commands Handler connecting to Client (command creation)

##### Specific flows
- Broadcast flow: Client → Router → UDP → Devices
- Unicast flow: Client → Router → UDP → Specific Device
- Discovery flow: Device → UDP → Router → Devices Manager
- Management flow: Devices Manager → Groups Manager

Key Architectural Points to Highlight:

##### Message Flow
**[Client] → [Router] → [UDP Socket] → [LIFX Device]**
- ↑
**[Devices Manager] ─┘**

##### Device Discovery
**[LIFX Device] → [UDP Socket] → [Router] → [Devices Manager]**
- ↓
- [Groups Manager]

##### Command Processing
**[Commands] → [Client] → [Router] → [Encoding] → [UDP Socket]**

Important Implementation Details to Note:

##### Socket Independence
- How the library is socket-agnostic
- "bring your own socket" philosophy

##### Multi-Client Support
- How multiple clients can share one router
- Source value management

##### Broadcasting vs Unicasting
- Demonstrate the two communication patterns
- How they can use separate sockets

- Message types (broadcast/unicast)
- Connection types (required/optional)
- Component responsibilities
- State management flow

- High-level architecture (showing main components)
- Implementation details (showing message flow)
- Integration points (showing external interfaces)
- Scalability aspects (showing multi-client support)

Remember to include notes about:
- Zero dependencies
- Runtime environment compatibility (Node.js, Bun, Deno)
- Optional features (groups, multiple clients)
- Error handling and retry mechanisms

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles
    classDef core fill:#a8d1f0,stroke:#333
    classDef support fill:#f9e79f,stroke:#333
    classDef external fill:#90EE90,stroke:#333
    classDef device fill:#D3D3D3,stroke:#333
    classDef socket fill:#98FB98,stroke:#333

    %% Components
    Client["Client Module"]:::core
    Router["Router Module"]:::core
    DevicesManager["Devices Manager"]:::core
    GroupsManager["Groups Manager"]:::core
    Commands["Commands Handler"]:::support
    Encoding["Encoding Module"]:::support
    Utils["Utils"]:::support
    Constants["Constants"]:::support
    UDPSocket["UDP Socket Interface"]:::socket
    LIFX1["LIFX Device 1"]:::device
    LIFX2["LIFX Device 2"]:::device
    LIFX3["LIFX Device 3"]:::device

    %% Relationships
    Client --> |"Commands"| Router
    Router <--> |"Message Flow"| UDPSocket
    DevicesManager --> |"Device Info"| Router
    GroupsManager --> |"Group Info"| DevicesManager
    Commands --> |"Command Creation"| Client
    Encoding --> |"Message Encoding"| Router
    Utils --> Router
    Utils --> DevicesManager
    Constants --> Router
    Constants --> Commands
    UDPSocket --> |"Discovery"| LIFX1
    UDPSocket --> |"Discovery"| LIFX2
    UDPSocket --> |"Discovery"| LIFX3

    %% Click Events
    click Client "https://github.com/jmmoser/lifxlan/blob/main/src/client.js"
    click Router "https://github.com/jmmoser/lifxlan/blob/main/src/router.js"
    click DevicesManager "https://github.com/jmmoser/lifxlan/blob/main/src/devices.js"
    click GroupsManager "https://github.com/jmmoser/lifxlan/blob/main/src/groups.js"
    click Commands "https://github.com/jmmoser/lifxlan/blob/main/src/commands.js"
    click Encoding "https://github.com/jmmoser/lifxlan/blob/main/src/encoding.js"
    click Utils "https://github.com/jmmoser/lifxlan/blob/main/src/utils.js"
    click Constants "https://github.com/jmmoser/lifxlan/blob/main/src/constants.js"

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Supporting Modules"]:::support
        L3["External Interfaces"]:::socket
        L4["Physical Devices"]:::device
    end
```
