# coapjs/node-coap architecture

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

- Interactive diagram: https://gitdiagram.com/coapjs/node-coap
- Repository: https://github.com/coapjs/node-coap

## Overview

##### Main Components

a. Core Components:
- CoAP Client
- CoAP Server
- Agent System (handles UDP socket management)
- Message Handling Layer
- Option Processing System
- Observe Pattern Implementation
- Proxy Component
- Caching System

b. Supporting Components:
- Block-wise Transfer Handler
- Retransmission Manager
- Stream Management
- Format Registry
- Option Registry

##### Key Relationships to Highlight

- Client-Server Communication Flow
- Message Processing Pipeline
- Observer Pattern Implementation
- Proxy Request Routing
- Block-wise Transfer Process

##### Specific Implementation Details

- Blue rectangles: Core components
- Green rectangles: Stream handling
- Yellow diamonds: Decision points
- Red hexagons: Error handling
- Purple circles: Socket management
- Orange rectangles: Option processing

##### Data Flow Representation

- Request/Response flow
- Observer pattern updates
- Proxy redirections
- Block-wise transfers
- Retransmission paths

##### Component Details

Client Side:
**[Request API] → [Option Processing] → [Message Formatter] → [Agent] → [UDP Socket]**

Server Side:
**[UDP Socket] → [Agent] → [Message Parser] → [Request Handler] → [Response Formatter]**

##### Technical Notes

Annotations for:
- Default port (5683)
- Supported CoAP RFCs
- Content format registrations
- Timing configurations
- Socket management details

##### Implementation Specifics

Highlight:
- Stream implementation
- Message types (CON, NON, ACK, RST)
- Option processing
- Format handling
- Proxy behavior

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Application Layer"
        API[API Interface]
        CS[CoAP Server]:::server
        CC[CoAP Client]
    end

    subgraph "Protocol Implementation Layer"
        AG[Agent System]:::agent
        MH[Message Handler]:::message
        OP[Option Processor]:::option
        OB[Observe Pattern]:::observe
        PR[Proxy Component]
        CA[Cache System]:::cache
    end

    subgraph "Transfer Layer"
        BW[Block-wise Transfer]:::block
        RM[Retransmission Manager]:::retry
        SM[Stream Management]
        FR[Format Registry]
        OR[Option Registry]
    end

    subgraph "Network Layer"
        UDP[UDP Sockets]
        IP[IPv4/IPv6 Handler]
    end

    subgraph "Support Systems"
        PM[Parameter Manager]:::params
        HU[Helper Utilities]:::helper
        MW[Middleware System]:::middleware
        CM[Core Models]:::models
    end

    %% Connections
    API --> CS
    API --> CC
    CS --> AG
    CC --> AG
    AG --> MH
    MH --> OP
    OP --> OB
    MH --> PR
    PR --> CA
    MH --> BW
    BW --> RM
    RM --> SM
    SM --> FR
    OP --> OR
    AG --> UDP
    UDP --> IP

    %% Support System Connections
    MH --> PM
    MH --> HU
    CS --> MW
    MH --> CM

    %% Styling
    classDef server fill:#a8e6cf
    classDef agent fill:#dcedc1
    classDef message fill:#ffd3b6
    classDef option fill:#ffaaa5
    classDef observe fill:#ff8b94
    classDef cache fill:#98ddca
    classDef block fill:#d4a5a5
    classDef retry fill:#ffb6b9
    classDef params fill:#e6b566
    classDef helper fill:#b6e2d3
    classDef middleware fill:#fae3d9
    classDef models fill:#bbded6

    %% Click Events
    click CS "https://github.com/coapjs/node-coap/blob/master/lib/server.ts"
    click AG "https://github.com/coapjs/node-coap/blob/master/lib/agent.ts"
    click MH "https://github.com/coapjs/node-coap/blob/master/lib/incoming_message.ts"
    click OP "https://github.com/coapjs/node-coap/blob/master/lib/option_converter.ts"
    click OB "https://github.com/coapjs/node-coap/blob/master/lib/observe_read_stream.ts"
    click CA "https://github.com/coapjs/node-coap/blob/master/lib/cache.ts"
    click BW "https://github.com/coapjs/node-coap/blob/master/lib/block.ts"
    click RM "https://github.com/coapjs/node-coap/blob/master/lib/retry_send.ts"
    click PM "https://github.com/coapjs/node-coap/blob/master/lib/parameters.ts"
    click HU "https://github.com/coapjs/node-coap/blob/master/lib/helpers.ts"
    click MW "https://github.com/coapjs/node-coap/blob/master/lib/middlewares.ts"
    click CM "https://github.com/coapjs/node-coap/blob/master/models/models.ts"
```
