# coderomm/collabydraw architecture

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

- Interactive diagram: https://gitdiagram.com/coderomm/collabydraw
- Repository: https://github.com/coderomm/collabydraw

## Overview

##### Project Overview & Purpose
- CollabyDraw is a web-based collaborative whiteboard application with a focus on real-time, encrypted drawing sessions.
- The system is split into multiple concerns including a Next.js frontend application (with its own canvas engine and UI components), a dedicated WebSocket server for real-time syncing, and a database layer (managed via Prisma) for persistence and authentication state.

##### Main Components
a. Client/Frontend Application:
- This is implemented in the CollabyDraw app under apps/collabydraw.
- It utilizes a modern React-based framework (Next.js) with TypeScript, Tailwind CSS, and a custom canvas engine.
- The “app” directory is organized into layouts (auth, main), API routes (for authentication via NextAuth), and UI components (drawing tools, sidebars, menus, etc.).
- Additional client-side libraries such as Rough.js and Perfect-freehand for sketch effects.

b. Canvas Engine:
- It is crucial to highlight that the canvas engine supports encryption and decryption routines on the client side – the encryption key is parsed from a URL fragment and is never sent to the server.

c. Realtime WebSocket Service:
- Located in apps/ws, this service powers the live collaboration aspect.
- It handles real-time bi-directional communication with the frontend, routing drawing data over WebSocket.
- It integrates with the application’s message queuing mechanism for auto retry on connection issues.

d. Backend API Endpoints:
- Exposed through Next.js API routes (e.g., apps/collabydraw/app/api/auth/[...nextauth]) to manage authentication and possibly session management.
- This layer is distinct from the WebSocket service and provides security and auth validation.

e. Database/Prisma Layer:
- Managed in the packages/db folder built with Prisma, it includes the schema and migrations.
- This service is responsible for storing room details, authentication data (if applicable), and other persistence-related configurations.

f. Shared Libraries & Configurations:
- Packages like packages/common, packages/ui, and others denote shared utilities, UI components, and configuration (ESLint, TypeScript) that help maintain consistency across the monorepo.

##### Relationships & Interactions
- The user’s actions on the frontend (drawing, editing, authentication) trigger processes in the Client Application.
- The Canvas Engine (client-side) handles rendering and encryption/decryption tasks locally.
- API calls for authentication and user management flow between the frontend’s API routes and external auth services.
- Backend communication with the Database is done via Prisma – this may include storing room sessions or user events.

##### Architectural Patterns & Design Principles
- The overall architecture follows a modular, microservices-like pattern with separate concerns:
- A full-stack Next.js client for UI and API endpoints.
- A distinct WebSocket server for real-time communication.
- A dedicated, managed database using Prisma.
- “privacy by design” where encryption logic is solely client-side.

## Diagram (Mermaid)

```mermaid
graph TD
    %% User Actor
    User(("User (Browser)")):::user

    %% Client-Side Components
    subgraph "Client Side"
        FE("Frontend Application\n(Next.js, React, TS, Tailwind)"):::frontend
        CE("Canvas Engine\n(client-side encryption, rendering, message queue)"):::frontend
        FE -->|"includes"| CE
    end

    %% Server-Side Components
    subgraph "Server Side"
        WS("Realtime WebSocket Service"):::realtime
        API("Backend API Endpoints\n(Next.js API for auth)"):::backend
    end

    %% Persistence Component
    subgraph "Persistence"
        DB("Database (Prisma)"):::database
    end

    %% Shared Libraries & Configurations
    subgraph "Shared Libraries & Configurations"
        CL("Common Library"):::shared
        UI("UI Library"):::shared
    end

    %% Interactions
    User -->|"initiates"| FE
    FE -->|"sendsDrawingData"| WS
    FE -->|"makesAuthCalls"| API
    API -->|"readsWrites"| DB
    WS -->|"syncsBack"| FE
    FE ---|"clientSideEncryption\n(URL-key not transmitted)"| CE

    %% Shared Support Arrows
    CL --- FE
    UI --- FE
    CL --- API
    UI --- FE

    %% Legend (Optional)
    Legend1["Legend:\n- Rectangles: Modules\n- Cylinder-like: Database\n- Colors: Frontend (blue), Server (green/orange), Database (yellow), Shared (orange), User (gray)"]:::legend
    User --- Legend1

    %% Click Events
    click FE "https://github.com/coderomm/collabydraw/tree/main/apps/collabydraw"
    click CE "https://github.com/coderomm/collabydraw/tree/main/apps/collabydraw/canvas-engine"
    click WS "https://github.com/coderomm/collabydraw/tree/main/apps/ws"
    click API "https://github.com/coderomm/collabydraw/blob/main/apps/collabydraw/app/api/auth/[...nextauth]/route.ts"
    click DB "https://github.com/coderomm/collabydraw/blob/main/packages/db/prisma/schema.prisma"
    click CL "https://github.com/coderomm/collabydraw/tree/main/packages/common"
    click UI "https://github.com/coderomm/collabydraw/tree/main/packages/ui"

    %% Styling classes
    classDef frontend fill:#b3e5fc,stroke:#0277bd,stroke-width:2px;
    classDef realtime fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef backend fill:#ffccbc,stroke:#d84315,stroke-width:2px;
    classDef database fill:#fff9c4,stroke:#f57f17,stroke-width:2px,stroke-dasharray: 5 5;
    classDef shared fill:#ffe082,stroke:#ff8f00,stroke-width:2px;
    classDef user fill:#e0e0e0,stroke:#757575,stroke-width:2px;
    classDef legend fill:#d1c4e9,stroke:#673ab7,stroke-width:2px,stroke-dasharray: 3 3;
```
