πŸ—οΈ Architecture - arilonUK/iotagentmesh GitHub Wiki

Architecture Overview

This document provides a comprehensive overview of the IoT Agent Mesh system architecture, including component relationships, data flow, and design decisions.

System Architecture

IoT Agent Mesh follows a modern web application architecture with a React frontend, Supabase backend, and real-time data synchronization capabilities.

High-Level Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   IoT Devices   β”‚    β”‚   Web Client    β”‚    β”‚  Mobile App     β”‚
β”‚                 β”‚    β”‚   (React)       β”‚    β”‚   (Future)      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚                      β”‚                      β”‚
          β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
          β”‚              β”‚           Load Balancer              β”‚
          β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚                      β”‚
          β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚              β”‚         Supabase API Gateway        β”‚
          β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚                      β”‚
          └──────────────────────┼─────────────────────────────────┐
                                 β”‚                                 β”‚
                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                 β”‚         Supabase Services       β”‚     β”‚  Edge Functions   β”‚
                 β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚     β”‚                   β”‚
                 β”‚  β”‚      PostgreSQL DB          β”‚ β”‚     β”‚  - Webhooks       β”‚
                 β”‚  β”‚   β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚ β”‚     β”‚  - Data Processingβ”‚
                 β”‚  β”‚   β”‚Orgs β”‚Usersβ”‚Devices  β”‚   β”‚ β”‚     β”‚  - Integrations   β”‚
                 β”‚  β”‚   β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚ β”‚     β”‚  - Alerts         β”‚
                 β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
                 β”‚  β”‚      Realtime Engine        β”‚ β”‚
                 β”‚  β”‚   (WebSocket/Server-Sent)   β”‚ β”‚
                 β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
                 β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
                 β”‚  β”‚         Auth Service        β”‚ β”‚
                 β”‚  β”‚     (JWT + RLS Policies)    β”‚ β”‚
                 β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
                 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Core Components

Frontend Architecture

The React frontend is built using modern patterns and best practices:

Component Structure

src/
β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ ui/                 # Base UI components (shadcn/ui)
β”‚   β”‚   β”œβ”€β”€ button.tsx
β”‚   β”‚   β”œβ”€β”€ input.tsx
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ layout/            # Layout components
β”‚   β”‚   β”œβ”€β”€ Header.tsx
β”‚   β”‚   β”œβ”€β”€ Sidebar.tsx
β”‚   β”‚   └── Layout.tsx
β”‚   β”œβ”€β”€ features/          # Feature-specific components
β”‚   β”‚   β”œβ”€β”€ organizations/
β”‚   β”‚   β”œβ”€β”€ agents/
β”‚   β”‚   β”œβ”€β”€ telemetry/
β”‚   β”‚   └── alerts/
β”‚   └── common/            # Shared components
β”œβ”€β”€ hooks/                 # Custom React hooks
β”‚   β”œβ”€β”€ useAuth.ts
β”‚   β”œβ”€β”€ useRealtime.ts
β”‚   └── useApi.ts
β”œβ”€β”€ lib/                   # Utilities and configurations
β”‚   β”œβ”€β”€ supabase.ts
β”‚   β”œβ”€β”€ utils.ts
β”‚   └── constants.ts
β”œβ”€β”€ types/                 # TypeScript definitions
β”‚   β”œβ”€β”€ database.ts
β”‚   β”œβ”€β”€ api.ts
β”‚   └── common.ts
└── pages/                 # Route components
    β”œβ”€β”€ Dashboard.tsx
    β”œβ”€β”€ Organizations.tsx
    └── Agents.tsx

State Management Strategy

  • Local State: React hooks (useState, useReducer) for component-level state
  • Server State: Supabase real-time subscriptions with custom hooks
  • Global State: React Context for authentication and user preferences
  • Form State: React Hook Form for form management and validation

Component Design Patterns

  • Composition over Inheritance: Flexible component composition
  • Container/Presenter Pattern: Separation of logic and presentation
  • Custom Hooks: Reusable business logic extraction
  • Error Boundaries: Graceful error handling at component boundaries

Backend Architecture (Supabase)

Database Design

The PostgreSQL database uses a multi-tenant architecture with row-level security (RLS):

-- Core Tables Structure
organizations (
  id UUID PRIMARY KEY,
  name TEXT NOT NULL,
  slug TEXT UNIQUE,
  settings JSONB,
  created_at TIMESTAMP,
  updated_at TIMESTAMP
)

users (
  id UUID REFERENCES auth.users,
  organization_id UUID REFERENCES organizations,
  role TEXT CHECK (role IN ('admin', 'user', 'viewer')),
  profile JSONB,
  created_at TIMESTAMP
)

agents (
  id UUID PRIMARY KEY,
  organization_id UUID REFERENCES organizations,
  name TEXT NOT NULL,
  type TEXT NOT NULL,
  configuration JSONB,
  status TEXT DEFAULT 'offline',
  last_seen TIMESTAMP,
  created_at TIMESTAMP
)

telemetry (
  id UUID PRIMARY KEY,
  agent_id UUID REFERENCES agents,
  timestamp TIMESTAMP DEFAULT NOW(),
  data JSONB NOT NULL,
  processed_at TIMESTAMP
)

alerts (
  id UUID PRIMARY KEY,
  organization_id UUID REFERENCES organizations,
  rule_id UUID REFERENCES alert_rules,
  agent_id UUID REFERENCES agents,
  severity TEXT,
  message TEXT,
  status TEXT DEFAULT 'active',
  created_at TIMESTAMP
)

Row-Level Security (RLS) Policies

-- Organizations: Users can only access their own organization
CREATE POLICY "org_access" ON organizations
  FOR ALL USING (id IN (
    SELECT organization_id FROM users WHERE id = auth.uid()
  ));

-- Agents: Organization-scoped access
CREATE POLICY "agent_access" ON agents
  FOR ALL USING (organization_id IN (
    SELECT organization_id FROM users WHERE id = auth.uid()
  ));

-- Role-based access control
CREATE POLICY "admin_full_access" ON users
  FOR ALL USING (
    organization_id IN (
      SELECT organization_id FROM users 
      WHERE id = auth.uid() AND role = 'admin'
    )
  );

Edge Functions

Server-side processing for complex operations:

// Webhook handler for IoT device data
export async function handleDeviceWebhook(req: Request) {
  const { agent_id, telemetry_data } = await req.json();
  
  // Validate and process telemetry
  const processedData = await processTelemetry(telemetry_data);
  
  // Store in database
  await supabase
    .from('telemetry')
    .insert({
      agent_id,
      data: processedData,
      timestamp: new Date().toISOString()
    });
  
  // Check alert rules
  await evaluateAlertRules(agent_id, processedData);
  
  return new Response('OK', { status: 200 });
}

Data Flow

Real-time Data Synchronization

  1. Device Telemetry Flow:

    IoT Device β†’ Webhook/API β†’ Edge Function β†’ PostgreSQL β†’ Realtime β†’ Frontend
    
  2. User Action Flow:

    Frontend β†’ Supabase API β†’ PostgreSQL β†’ RLS Check β†’ Realtime β†’ Other Clients
    
  3. Alert Processing Flow:

    Telemetry Data β†’ Alert Rules Engine β†’ Alert Creation β†’ Notification Service β†’ Users
    

Authentication Flow

sequenceDiagram
    participant User
    participant Frontend
    participant Supabase Auth
    participant Database
    
    User->>Frontend: Login Request
    Frontend->>Supabase Auth: Authenticate
    Supabase Auth->>Database: Validate User
    Database->>Supabase Auth: User Data + RLS Context
    Supabase Auth->>Frontend: JWT Token + User Profile
    Frontend->>User: Authenticated Session
    
    Note over Frontend, Database: All subsequent requests include JWT
    Frontend->>Database: API Requests (with JWT)
    Database->>Database: Apply RLS Policies
    Database->>Frontend: Filtered Results

Security Architecture

Authentication & Authorization

  • JWT-based Authentication: Secure token-based authentication
  • Row-Level Security: Database-level access control
  • Role-Based Access Control: Hierarchical permission system
  • Multi-tenant Isolation: Organization-level data separation

Data Protection

  • Encryption at Rest: PostgreSQL encryption for sensitive data
  • Encryption in Transit: TLS 1.3 for all communications
  • Input Validation: Server-side validation for all inputs
  • SQL Injection Protection: Parameterized queries and ORM

API Security

  • Rate Limiting: Request throttling and DDoS protection
  • CORS Configuration: Proper cross-origin resource sharing
  • API Key Management: Secure API key rotation and management
  • Webhook Verification: Signed webhook payloads

Scalability Considerations

Horizontal Scaling

  • Stateless Architecture: No server-side session state
  • Database Connection Pooling: Efficient connection management
  • CDN Integration: Static asset delivery optimization
  • Edge Function Deployment: Global function distribution

Performance Optimization

  • Database Indexing: Optimized queries with proper indexes
  • Real-time Optimization: Efficient WebSocket connection management
  • Caching Strategy: Application-level and database-level caching
  • Bundle Optimization: Code splitting and lazy loading

Monitoring & Observability

  • Application Metrics: Performance and usage analytics
  • Error Tracking: Automated error detection and reporting
  • Log Aggregation: Centralized logging and analysis
  • Health Checks: System health monitoring and alerting

Technology Decisions

Why React + TypeScript?

  • Developer Experience: Excellent tooling and IDE support
  • Type Safety: Reduced runtime errors and better refactoring
  • Ecosystem: Rich library ecosystem and community support
  • Performance: Virtual DOM and optimization features

Why Supabase?

  • Rapid Development: Backend-as-a-Service for faster development
  • Real-time Capabilities: Built-in WebSocket support
  • PostgreSQL: Full SQL database with ACID compliance
  • Automatic API Generation: RESTful API from database schema

Why Tailwind CSS?

  • Utility-First Approach: Faster UI development
  • Consistent Design System: Unified spacing and color schemes
  • Performance: Purged CSS for minimal bundle size
  • Flexibility: Easy customization and theming

See Also