# trailheadapps/ecars architecture

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

- Interactive diagram: https://gitdiagram.com/trailheadapps/ecars
- Repository: https://github.com/trailheadapps/ecars

## Overview

##### Main Components
- Salesforce Org: Acts as the central data store (sales, service, leads, vehicles, etc.) and houses key business logic, Lightning Web Components, Apex controllers, flows, triggers, and custom objects.
- Heroku Applications:
– ecars-mqtt-broker: Provides the MQTT messaging layer for IoT interactions (e.g., car diagnostic data).
– ecars-realtime: Streams IoT sensor data, simulates sensors, and interfaces with a Postgres database for persistence.
– ecars-pwa: A progressive web app offering the customer-facing interface, integrating Lightning web components (and using Node scripts for tasks like Postgres table creation).
– ecars-services: Microservices that process tasks such as PDF generation, web push notifications, and composite API calls interacting with Salesforce.
- External Databases & Add-ons: Heroku Postgres databases used by ecars-realtime and PWA for storing and querying data.
- Integration Points/Configuration: Named credentials, environment variables (VAPID keys, Salesforce credentials), and configuration files that tie these systems together.
- Event-Driven/Integration Layers: Change Data Capture from Salesforce, WebSockets (for real-time updates), and MQTT event flows that connect disparate parts of the system.

##### Relationships and Data Flow
- How the PWA gathers customer input and sends requests through composite APIs to Salesforce.
- That the microservices app (ecars-services) interacts with Salesforce (using named credentials) to generate PDFs and send web push notifications.
- That Heroku Postgres is attached as an add-on to the streaming and PWA apps and is used for persisting IoT data and app state.
- Integration points such as updating Salesforce with external data from Heroku apps and receiving live data via WebSocket connections.

##### Architectural Patterns and Important Design Principles
- Monorepo Architecture: Demonstrate that several applications are managed in one repository for consistency.
- Event-Driven Communication: Emphasize that systems communicate through events (via MQTT, WebSockets, and change events in Salesforce), which promotes loose coupling.
- Cloud-Native Deployments: Note the deployment via Heroku with respective add-ons and the integration with Salesforce using scratch orgs and metadata deployments.

##### Representative Technologies and Frameworks
- Salesforce (including Lightning Web Components, Apex, Change Data Capture)
- Node.js (used across Heroku apps)
- MQTT protocol for IoT messaging and secure WebSockets (wss://)
- Heroku deployment and Postgres as a database
- Tools such as Lerna for monorepo management and Jest for testing

##### Specific Elements
– Sensor data flow: Vehicle devices → MQTT Broker → Real-time Streaming Service → Salesforce (and optionally into Postgres for persistence).
– Customer transactions: PWA → Salesforce (via Composite Graph API).
– Microservice interactions: Microservices application → Salesforce for PDF generation and Web Push notifications.
- Optionally, include configuration elements (e.g., named credentials, environment variables) as notes or annotations.
- Use callouts to denote where event-driven patterns are applied and where technologies like WebSockets, MQTT, and Change Data Capture are used.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Client Devices
    subgraph "Clients"
        Vehicle["Vehicle Devices"]:::device
        Customer["Customer Devices"]:::device
    end

    %% Heroku Applications
    subgraph "Heroku Applications"
        Mqtt["Heroku MQTT Broker (ecars-mqtt-broker)"]:::heroku
        Realtime["Heroku Realtime Service (ecars-realtime)"]:::heroku
        Pwa["Heroku PWA (ecars-pwa)"]:::heroku
        Services["Heroku Microservices (ecars-services)"]:::heroku
    end

    %% Datastores & Add-ons
    subgraph "Datastores"
        Postgres["Heroku Postgres"]:::database
    end

    %% Core System - Salesforce Org
    Salesforce["Salesforce Org"]:::salesforce

    %% Data Flow Relationships
    Vehicle -->|"publishes_data_via_MQTT"| Mqtt
    Mqtt -->|"forwards_sensor_data"| Realtime
    Realtime -->|"updates_via_Change_Data_Capture"| Salesforce
    Realtime -->|"stores_sensor_data"| Postgres

    Customer -->|"interacts_via_PWA"| Pwa
    Pwa -->|"sends_composite_API_requests"| Salesforce
    Pwa -->|"maintains_app_state"| Postgres

    Services -->|"calls_for_PDF_and_notifications"| Salesforce

    %% Click Events
    click Salesforce "https://github.com/trailheadapps/ecars/tree/main/force-app/main/default"
    click Mqtt "https://github.com/trailheadapps/ecars/tree/main/apps/ecars-mqtt-broker"
    click Realtime "https://github.com/trailheadapps/ecars/tree/main/apps/ecars-realtime"
    click Pwa "https://github.com/trailheadapps/ecars/tree/main/apps/ecars-pwa"
    click Services "https://github.com/trailheadapps/ecars/tree/main/apps/ecars-services"

    %% Styles
    classDef salesforce fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef heroku fill:#8EC4E6,stroke:#333,stroke-width:2px;
    classDef device fill:#BDE0FE,stroke:#333,stroke-width:2px;
    classDef database fill:#C7F9CC,stroke:#333,stroke-width:2px;
```
