# anthonejc/chat-e2ee architecture

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

- Interactive diagram: https://gitdiagram.com/anthonejc/chat-e2ee
- Repository: https://github.com/anthonejc/chat-e2ee

## Overview

1. **Repository shape**
   - This is a monorepo with three main runtime surfaces: the **browser UI** in `client/`, the **Node/Express backend** in `backend/`, and a reusable **client-side SDK** in `service/`.
   - The root `app.ts` / `index.ts` files are the server entrypoints; in production the backend also serves the built frontend assets.

2. **Primary product boundary**
   - The system is designed for **ephemeral, disposable 1:1 encrypted chat sessions**.
   - A key architectural constraint is that **private keys never leave the user’s device**; the backend relays encrypted content and does not act as a decryption boundary.

3. **Frontend/UI layer**
   - `client/` is a lightweight React-based UI that consumes the SDK rather than embedding most protocol logic itself.
   - This keeps the presentation layer thin: UI interactions are separated from encryption, signaling, and transport logic.

4. **SDK layer**
   - `service/` is the core client-facing package (`@chat-e2ee/service`) intended for both the bundled UI and custom frontends.
   - It wraps cryptography, link/session handling, socket communication, and WebRTC call setup into a single client library.
   - Key modules include `cryptoAES.ts`, `cryptoRSA.ts`, `socket/`, `webrtc.ts`, `webrtcSession.ts`, and helpers for links, users, and message sending.

5. **Backend API layer**
   - `backend/api/` groups the HTTP-facing business endpoints by concern:
     - `chatHash/` for link/PIN/channel validation and session identity,
     - `messaging/` for message-related operations,
     - `call/` for call/session setup.
   - `backend/middleware/asyncHandler.ts` suggests the API is built around async Express handlers with centralized error wrapping.

6. **Realtime signaling and presence**
   - `backend/socket.io/` is the realtime backbone.
   - `clients.ts` tracks connected users/participants, `listeners.ts` wires event handlers, and `index.ts` initializes the socket server.
   - This channel is used for low-latency relay and for WebRTC signaling, not as a message store.

7. **Session and channel model**
   - The app centers on a **chat hash / link / PIN** concept, implemented in `backend/api/chatHash/utils/`.
   - Utilities such as `link.ts`, `pin.ts`, and `validateChannel.ts` indicate a session bootstrapping flow where two users join the same channel after sharing a generated link or PIN.
   - This is the primary identity mechanism; there is no username/password account model.

8. **Data storage strategy**
   - `backend/db/inMemDB.ts` implies the backend uses **in-memory state**, not a durable database.
   - That matches the product goal: minimal retention, no chat history after the session ends, and no long-lived server-side user records.
   - `backend/db/const.ts` likely holds shared session/status constants used across API and socket code.

9. **Cryptography boundary**
   - The SDK handles encryption on the client side using browser cryptography and helper implementations in `service/src/cryptoAES.ts` and `service/src/cryptoRSA.ts`.
   - The README states that private keys are generated on-device and never uploaded; the backend only sees encrypted payloads.
   - This makes the server a transport/signaling relay rather than a trusted decryption service.

10. **WebRTC audio path**
   - Audio calling is a separate realtime path from text chat, implemented in `service/src/webrtc.ts` and `service/src/webrtcSession.ts`.
   - `backend/api/call/session.ts` and socket signaling coordinate session establishment, while media itself is intended to flow peer-to-peer.
   - The README notes experimental end-to-end audio encryption using `RTCRtpSender.createEncodedStreams`, which is a notable browser capability constraint.

11. **External service integrations**
   - `backend/external/imgbb.ts`, `imgur.ts`, and `uploadImage.ts` indicate optional image-upload integration with third-party hosts.
   - These are ancillary to the core chat path and represent the only obvious external data boundary beyond the backend itself.

12. **Build, deployment, and delivery**
   - The repo includes Docker support under `docker/`, a native wrapper under `native/`, and CI/publish workflows in `.github/workflows/`.
   - This suggests the project can be run locally, containerized, or packaged as a reusable service/SDK.
   - In production, the backend serves the compiled frontend, so the deployable unit is effectively a single Node service plus static assets.

13. **Verification and quality gates**
   - Unit tests exist around critical protocol utilities and crypto (`service/src/*.test.ts`, backend chat-hash tests), and there is a Playwright e2e flow for joining a session.
   - These tests reflect the architecture’s most sensitive seams: session joining, channel validation, and cryptographic helpers.

Main entry points: [`service/src/sdk.ts`](https://github.com/anthonejc/chat-e2ee/blob/master/service/src/sdk.ts) (Client SDK), [`backend/api/index.ts`](https://github.com/anthonejc/chat-e2ee/blob/master/backend/api/index.ts) (HTTP API), [`backend/socket.io/index.ts`](https://github.com/anthonejc/chat-e2ee/blob/master/backend/socket.io/index.ts) (Socket relay), [`app.ts`](https://github.com/anthonejc/chat-e2ee/blob/master/app.ts) (Server entry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_frontend["Frontend"]
  node_node_client_ui["React UI<br/>browser-app<br/>[app.ts]"]
  node_node_service_sdk["Client SDK<br/>browser-library<br/>[sdk.ts]"]
  node_node_crypto{{"Crypto<br/>[cryptoAES.ts]"}}
  node_node_links["Links & users<br/>session-client<br/>[getChatLink.ts]"]
  node_node_socket_client(("Socket client<br/>realtime-client<br/>[socket.ts]"))
  node_node_webrtc(("WebRTC<br/>peer-connection<br/>[webrtc.ts]"))
  node_node_protocol["Protocol helpers<br/>client-utilities<br/>[types.ts]"]
end

subgraph group_group_backend["Backend"]
  node_node_root["Server entry<br/>node-runtime<br/>[app.ts]"]
  node_node_api["HTTP API<br/>express-api<br/>[index.ts]"]
  node_node_chat_hash["Chat hash<br/>session-bootstrap<br/>[index.ts]"]
  node_node_messaging_api["Messaging API<br/>message-endpoints<br/>[index.ts]"]
  node_node_call_api["Call API<br/>call-setup<br/>[session.ts]"]
  node_node_socket(("Socket relay<br/>realtime-signaling<br/>[index.ts]"))
  node_node_socket_clients["Clients<br/>presence-tracking<br/>[clients.ts]"]
  node_node_socket_listeners["Listeners<br/>event-handlers<br/>[listeners.ts]"]
  node_node_db[("In-memory DB<br/>ephemeral-state<br/>[inMemDB.ts]")]
  node_node_external["Image upload<br/>third-party-integrations<br/>[uploadImage.ts]"]
end

subgraph group_group_delivery["Delivery"]
  node_node_build["Build & deploy<br/>tooling"]
end

node_node_root -->|"boots"| node_node_api
node_node_root -->|"starts"| node_node_socket
node_node_root -->|"shares state"| node_node_db
node_node_api -->|"routes"| node_node_chat_hash
node_node_api -->|"routes"| node_node_messaging_api
node_node_api -->|"routes"| node_node_call_api
node_node_chat_hash -->|"reads/writes"| node_node_db
node_node_messaging_api -->|"stores transient"| node_node_db
node_node_call_api -->|"signals"| node_node_socket
node_node_socket -->|"tracks"| node_node_socket_clients
node_node_socket -->|"dispatches"| node_node_socket_listeners
node_node_socket_listeners -->|"updates"| node_node_db
node_node_client_ui -->|"uses"| node_node_service_sdk
node_node_service_sdk -->|"wraps"| node_node_crypto
node_node_service_sdk -->|"wraps"| node_node_links
node_node_service_sdk -->|"connects"| node_node_socket_client
node_node_service_sdk -->|"orchestrates"| node_node_webrtc
node_node_service_sdk -->|"depends on"| node_node_protocol
node_node_crypto -->|"encrypts for"| node_node_api
node_node_socket_client -->|"connects to"| node_node_socket
node_node_webrtc -->|"initiates"| node_node_call_api
node_node_external -->|"supports"| node_node_messaging_api
node_node_build -->|"packages"| node_node_root
node_node_build -->|"bundles"| node_node_client_ui
node_node_build -->|"publishes"| node_node_service_sdk

click node_node_root "https://github.com/anthonejc/chat-e2ee/blob/master/app.ts"
click node_node_api "https://github.com/anthonejc/chat-e2ee/blob/master/backend/api/index.ts"
click node_node_chat_hash "https://github.com/anthonejc/chat-e2ee/blob/master/backend/api/chatHash/index.ts"
click node_node_messaging_api "https://github.com/anthonejc/chat-e2ee/blob/master/backend/api/messaging/index.ts"
click node_node_call_api "https://github.com/anthonejc/chat-e2ee/blob/master/backend/api/call/session.ts"
click node_node_socket "https://github.com/anthonejc/chat-e2ee/blob/master/backend/socket.io/index.ts"
click node_node_socket_clients "https://github.com/anthonejc/chat-e2ee/blob/master/backend/socket.io/clients.ts"
click node_node_socket_listeners "https://github.com/anthonejc/chat-e2ee/blob/master/backend/socket.io/listeners.ts"
click node_node_db "https://github.com/anthonejc/chat-e2ee/blob/master/backend/db/inMemDB.ts"
click node_node_external "https://github.com/anthonejc/chat-e2ee/blob/master/backend/external/uploadImage.ts"
click node_node_client_ui "https://github.com/anthonejc/chat-e2ee/blob/master/client/app.ts"
click node_node_service_sdk "https://github.com/anthonejc/chat-e2ee/blob/master/service/src/sdk.ts"
click node_node_crypto "https://github.com/anthonejc/chat-e2ee/blob/master/service/src/cryptoAES.ts"
click node_node_links "https://github.com/anthonejc/chat-e2ee/blob/master/service/src/getChatLink.ts"
click node_node_socket_client "https://github.com/anthonejc/chat-e2ee/blob/master/service/src/socket/socket.ts"
click node_node_webrtc "https://github.com/anthonejc/chat-e2ee/blob/master/service/src/webrtc.ts"
click node_node_protocol "https://github.com/anthonejc/chat-e2ee/blob/master/service/src/public/types.ts"
click node_node_build "https://github.com/anthonejc/chat-e2ee/tree/master/docker/Dockerfile"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_client_ui,node_node_service_sdk,node_node_crypto,node_node_links,node_node_socket_client,node_node_webrtc,node_node_protocol toneBlue
class node_node_root,node_node_api,node_node_chat_hash,node_node_messaging_api,node_node_call_api,node_node_socket,node_node_socket_clients,node_node_socket_listeners,node_node_db,node_node_external toneAmber
class node_node_build toneMint
```

## Components

### Frontend

Browser UI and client SDK

- [React UI](https://github.com/anthonejc/chat-e2ee/blob/master/client/app.ts): browser-app · Thin browser presentation layer that consumes the SDK · `client/app.ts`
- [Client SDK](https://github.com/anthonejc/chat-e2ee/blob/master/service/src/sdk.ts): browser-library · Reusable client package wrapping crypto, links, sockets, and WebRTC · `service/src/sdk.ts`
- [Crypto](https://github.com/anthonejc/chat-e2ee/blob/master/service/src/cryptoAES.ts): client-crypto · Client-side AES and RSA helpers; private keys stay on-device · `service/src/cryptoAES.ts`
- [Links & users](https://github.com/anthonejc/chat-e2ee/blob/master/service/src/getChatLink.ts): session-client · Link generation, chat URL handling, user lookup, and session messaging helpers · `service/src/getChatLink.ts`
- [Socket client](https://github.com/anthonejc/chat-e2ee/blob/master/service/src/socket/socket.ts): realtime-client · Client-side socket communication used by the SDK · `service/src/socket/socket.ts`
- [WebRTC](https://github.com/anthonejc/chat-e2ee/blob/master/service/src/webrtc.ts): peer-connection · Peer-to-peer audio call setup and session management · `service/src/webrtc.ts`
- [Protocol helpers](https://github.com/anthonejc/chat-e2ee/blob/master/service/src/public/types.ts): client-utilities · UUID, logging, config context, and shared public types · `service/src/public/types.ts`

### Backend

Express API, realtime relay, and in-memory state

- [Server entry](https://github.com/anthonejc/chat-e2ee/blob/master/app.ts): node-runtime · Top-level Node entrypoints that bootstrap the app and serve the production build · `app.ts`
- [HTTP API](https://github.com/anthonejc/chat-e2ee/blob/master/backend/api/index.ts): express-api · Backend API surface grouped by chat hash, messaging, and call concerns · `backend/api/index.ts`
- [Chat hash](https://github.com/anthonejc/chat-e2ee/blob/master/backend/api/chatHash/index.ts): session-bootstrap · Link, PIN, and channel validation for joining disposable sessions · `backend/api/chatHash/index.ts`
- [Messaging API](https://github.com/anthonejc/chat-e2ee/blob/master/backend/api/messaging/index.ts): message-endpoints · HTTP operations for chat message flow · `backend/api/messaging/index.ts`
- [Call API](https://github.com/anthonejc/chat-e2ee/blob/master/backend/api/call/session.ts): call-setup · HTTP setup for audio call sessions · `backend/api/call/session.ts`
- [Socket relay](https://github.com/anthonejc/chat-e2ee/blob/master/backend/socket.io/index.ts): realtime-signaling · Socket.io server for presence, low-latency relay, and WebRTC signaling · `backend/socket.io/index.ts`
- [Clients](https://github.com/anthonejc/chat-e2ee/blob/master/backend/socket.io/clients.ts): presence-tracking · Tracks connected users and channel participants · `backend/socket.io/clients.ts`
- [Listeners](https://github.com/anthonejc/chat-e2ee/blob/master/backend/socket.io/listeners.ts): event-handlers · Wires socket events into backend behavior · `backend/socket.io/listeners.ts`
- [In-memory DB](https://github.com/anthonejc/chat-e2ee/blob/master/backend/db/inMemDB.ts): ephemeral-state · Transient server state for sessions and channels · `backend/db/inMemDB.ts`
- [Image upload](https://github.com/anthonejc/chat-e2ee/blob/master/backend/external/uploadImage.ts): third-party-integrations · Optional integrations for uploading images to external hosts · `backend/external/uploadImage.ts`

### Delivery

Build, deployment, and packaging

- [Build & deploy](https://github.com/anthonejc/chat-e2ee/tree/master/docker/Dockerfile): tooling · Docker, native packaging, and publish workflows for delivery · `docker/Dockerfile`

## Connections

- Server entry → HTTP API: boots · Server entrypoint mounts the HTTP API
- Server entry → Socket relay: starts · Server entrypoint initializes realtime signaling
- Server entry → In-memory DB: shares state · Backend runtime uses transient in-memory session state
- HTTP API → Chat hash: routes · API delegates session identity and join validation
- HTTP API → Messaging API: routes · API exposes message operations
- HTTP API → Call API: routes · API exposes call/session setup
- Chat hash → In-memory DB: reads/writes · Session bootstrap relies on ephemeral backend state
- Messaging API → In-memory DB: stores transient · Messaging metadata is kept only for the live session
- Call API → Socket relay: signals · Call setup coordinates via realtime channel
- Socket relay → Clients: tracks · Realtime server maintains connected participants
- Socket relay → Listeners: dispatches · Socket events are bound to handlers
- Listeners → In-memory DB: updates · Handlers mutate transient session state
- React UI → Client SDK: uses · UI delegates protocol logic to the SDK
- Client SDK → Crypto: wraps · SDK exposes client-side encryption primitives
- Client SDK → Links & users: wraps · SDK handles session links, users, and message flow
- Client SDK → Socket client: connects · SDK uses sockets for signaling and relay
- Client SDK → WebRTC: orchestrates · SDK sets up peer-to-peer audio sessions
- Client SDK → Protocol helpers: depends on · SDK shares public types and client utilities
- Crypto → HTTP API: encrypts for · Client encrypts payloads before sending them to the server
- Socket client → Socket relay: connects to · Client SDK speaks to backend realtime relay
- WebRTC → Call API: initiates · WebRTC session setup begins through call endpoints
- Image upload → Messaging API: supports · Optional image upload augments message flow
- Build & deploy → Server entry: packages · Delivery tooling builds the server artifact
- Build & deploy → React UI: bundles · Delivery tooling compiles frontend assets
- Build & deploy → Client SDK: publishes · SDK is packaged for reuse by other frontends
