# google/nearby architecture

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

- Interactive diagram: https://gitdiagram.com/google/nearby
- Repository: https://github.com/google/nearby

## Overview

##### Main Components
- Nearby Connections Module (folder: “connections/”): This is the core networking library. It covers device discovery, peer-to-peer connection management, data exchange, and cross-platform implementations (native C/C++, Swift, Dart, etc.).
- Nearby Presence Module (folder: “presence/”): This extends connections by providing mechanisms for authentication (identity model), proximity detection (via sensor fusion), and additional resource management.
- Nearby for Embedded Systems (folder: “embedded/”): A specialized, lightweight implementation for embedded environments, focusing on Fast Pair.
- Shared/Supporting Components:
– Proto definitions and generated code are found in “proto/”, “compiled_proto/”, and “internal/proto/”.
– The “sharing/” directory provides additional functionality for sharing data (files, attachments) and incorporates its own analytics, certificates, and connectivity nuances.
– Third-party libraries (in “third_party/”) are integrated for supporting functionalities like cryptography (mbedtls, micro-ecc), serialization (protobuf, json), and various secure communications protocols.

##### Architectural Patterns & Design Principles
- Layered Architecture: Indicate that on one side there is the low-level protocol/crypto layer (e.g., proto definitions, mbedtls, ukey2) and on top of that lie the higher-level logic for connection management and presence.
- Testability: The presence of many “test” directories (internal/test, unit tests for broadcast, encryption, etc.) indicates a well-decoupled design that relies on interface-based programming and dependency injection. You may annotate or separate these as supporting components.

##### Final Assembly

Happy diagramming!

## Diagram (Mermaid)

```mermaid
graph TD
    md["Mobile/Desktop Clients"]:::client
    md -->|"calls"| ncon1

    subgraph "Core Modules"
        subgraph "Nearby Connections"
            ncon1["Discovery Service"]:::core
            ncon2["Connection Manager"]:::core
            ncon3["Data Exchange & Encryption"]:::core
            ncon4["Platform Adapters (Swift, Dart)"]:::core
        end
        subgraph "Nearby Presence"
            npres1["Identity Manager"]:::enhancement
            npres2["Sensor Fusion Engine"]:::enhancement
            npres3["Service Controller"]:::enhancement
        end
        subgraph "Embedded Fast Pair"
            nemb1["Fast Pair Core"]:::embedded
        end
    end

    subgraph "Shared Components"
        pc["Protocol & Common"]:::shared
        share["Sharing Functionality"]:::shared
        tp["Third Party Libraries"]:::external
    end

    ncon2 -->|"utilizes"| pc
    ncon3 -->|"secured_by"| tp
    npres1 -->|"authenticates_with"| pc
    nemb1 -->|"communicates_via"| pc
    share -->|"augments"| ncon3
    npres3 -->|"extends"| ncon2
    npres3 -->|"interacts_with"| share

    click ncon1 "https://github.com/google/nearby/tree/main/connections/"
    click npres1 "https://github.com/google/nearby/tree/main/presence/"
    click nemb1 "https://github.com/google/nearby/tree/main/embedded/"
    click pc "https://github.com/google/nearby/tree/main/proto/"
    click share "https://github.com/google/nearby/tree/main/sharing/"
    click tp "https://github.com/google/nearby/tree/main/third_party/"

    classDef core fill:#e6f7ff,stroke:#1890ff,stroke-width:2px;
    classDef enhancement fill:#fffbe6,stroke:#faad14,stroke-width:2px;
    classDef embedded fill:#f6ffed,stroke:#52c41a,stroke-width:2px;
    classDef shared fill:#f9f0ff,stroke:#722ed1,stroke-width:2px;
    classDef external fill:#fff0f0,stroke:#ff4d4f,stroke-width:2px;
    classDef client fill:#e6fffb,stroke:#13c2c2,stroke-width:2px;
```
