# ameya-jarvis-07/p2p-file-sharing architecture

> Architecture diagram of the GitHub repository ameya-jarvis-07/p2p-file-sharing, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-02-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ameya-jarvis-07/p2p-file-sharing
- Repository: https://github.com/ameya-jarvis-07/p2p-file-sharing

## Overview

This is a **single-node web app** whose primary purpose is **peer-to-peer (P2P) file transfer** using **WebRTC DataChannels**, with a **Node/Express + Socket.IO signaling server**. The server’s job is *not* to proxy or store files; it only:
- serves the static frontend
- provides “room/presence” + signaling messages (offer/answer/ICE candidates) so browsers can establish a direct WebRTC connection

- **Signaling traffic goes through the server**
- **File bytes do not** (they flow directly peer-to-peer)

##### File structure analysis (map files to components)
From the file tree:

- `server.js`
Backend entrypoint. Likely:
- Express static hosting (`public/`)
- Socket.IO server for realtime peer discovery + WebRTC signaling

- `public/index.html`, `public/styles.css`
UI shell and styling.

- `public/client.js`
Core client logic:
- connects to Socket.IO signaling
- manages peer list/presence UI
- creates RTCPeerConnections, DataChannels
- chunks/sends files, receives/reassembles files
- progress reporting/backpressure

- `package.json`
Confirms runtime and dependencies (express, socket.io, etc.) and start command for deployment.

This is a “two-tier” app in terms of deployable units (server + browser client) but “three logical planes” in terms of networking:

##### README insights to reflect
- “**True P2P Transfer**”: bytes go peer↔peer over WebRTC DataChannels
- “**Server acts as signaling server**”
- “**No server storage**”
- “**STUN** for NAT traversal” (possibly TURN is not mentioned; show as optional/absent)

##### Socket.IO Signaling Server
4. (Optional) **In-memory peer registry / sessions** (even if implicit; it’s conceptually there)

**Client-side (per device/browser)**
##### WebRTC Stack
- RTCPeerConnection
- ICE agent
- DataChannel (the actual file transfer transport)
##### File API / Chunking pipeline
- chunk sizing strategy
- buffering / backpressure
- progress tracking
##### Received-file assembly & download (Blob creation + link)

**External/Internet infrastructure**
##### STUN server(s) (NAT traversal)
12. (Optional) **TURN server** as “not included / if needed in restrictive NATs” (mark as optional to avoid implying it exists)

##### Static asset delivery
- Browser → Server (HTTP GET `/`, `/client.js`, `/styles.css`)

##### Signaling & presence
- Browser ↔ Server (Socket.IO over WebSocket)
- “join / set name”
- “peer list updates”
- “offer / answer / ICE candidates”
- This is many-to-one (multiple clients connected to the signaling server).

##### P2P data plane (the critical one)
- Browser A ↔ Browser B (WebRTC DataChannel)

##### NAT traversal
- Each browser ↔ STUN server

Show:
- “P2P File Sharing Web App” as a system
- Users/devices as multiple browsers
- Render-hosted signaling server
- External STUN (and optional TURN)
Two distinct channels:
- signaling via server
- file transfer direct peer-to-peer

Containers:
- Node.js/Express/Socket.IO server (single container)
- Browser client (many instances)
External:
- STUN
Connections:
- HTTP, WebSocket, WebRTC

This is where WebRTC correctness shines. Include steps:

##### Important architectural patterns/principles to call out
- **Control plane vs data plane split**
- Control plane: Socket.IO signaling
- Data plane: WebRTC DataChannel (direct)
- **No server persistence** (explicitly annotate “no file storage” on server)
- **Event-driven realtime** (Socket.IO)
- **Chunked streaming + backpressure** (client-side pipeline)
- **Many-to-many connectivity** (peer discovery suggests N peers; each pair may form a connection)

- “Peer mesh”: each browser can establish DataChannels to multiple peers
- Use a hub-and-spoke only for signaling, not data

- **Node Signaling Server** → `server.js`
- **Client App** → `public/client.js`
- **UI** → `public/index.html`, `public/styles.css`
- **Build/Run** → `package.json` (`npm start`)

##### What *not* to accidentally imply
- Don’t include a database unless you confirm it exists in code.
- Don’t claim TURN is present unless configured; show it as optional.

- [ ] Distinct lanes for HTTP vs Socket.IO vs WebRTC
- [ ] STUN shown as external dependency for ICE/NAT traversal
- [ ] Multiple browsers shown (multi-device)
- [ ] Client-side chunking/buffering noted (since it’s a core feature)
- [ ] Deployment boundary (Render.com) shown around Node service

## Diagram (Mermaid)

```mermaid
flowchart TD
  %% =========================
  %% Diagram 1 — C4 Context
  %% =========================
  subgraph D1["Diagram 1 — System Context (Control plane vs Data plane)"]
    direction TB

    U1["User / Device A\n(Browser)"]:::user
    U2["User / Device B\n(Browser)"]:::user

    subgraph RENDER["Render.com (single Node web service)"]
      direction TB
      SCTX["P2P File Sharing Web App\n(Signaling server + Static hosting)\n\nNote: Signaling only; NO file storage\nNO file bytes proxied"]:::server
    end

    STUN1["STUN server(s)\n(External NAT traversal)"]:::external
    TURN1["TURN relay (optional)\n(Not included; fallback for restrictive NATs)"]:::externalOptional

    %% Channels
    U1 -->|"HTTP:GET / /client.js /styles.css"| SCTX
    U2 -->|"HTTP:GET / /client.js /styles.css"| SCTX

    U1 <-->|"Socket.IO:presence+SDP+ICE"| SCTX
    U2 <-->|"Socket.IO:presence+SDP+ICE"| SCTX

    U1 <-->|"WebRTCDataChannel:encryptedfilechunks\n(progressacks/backpressure)"| U2

    U1 <-->|"STUN:ICEdiscovery/binding"| STUN1
    U2 <-->|"STUN:ICEdiscovery/binding"| STUN1

    U1 -.->|"Optional:relayifneeded"| TURN1
    U2 -.->|"Optional:relayifneeded"| TURN1
  end

  %% =========================
  %% Diagram 2 — Containers
  %% =========================
  subgraph D2["Diagram 2 — Container / Runtime View (single deployable server + many browsers)"]
    direction TB

    subgraph CLIENTS["Client-side (many instances)"]
      direction TB

      subgraph B1["Browser A"]
        direction TB
        B1UI["UI Shell\n(index.html + styles.css)"]:::frontend
        B1APP["Client App Logic\n(peer list + WebRTC + file send/receive)\n(client.js)"]:::frontend
        B1SIO["Socket.IO Client\n(signaling channel)"]:::control
        B1WRTC["WebRTC Stack\n(RTCPeerConnection + ICE + DataChannel)"]:::data
        B1PIPE["File API + Chunking Pipeline\n(buffering/backpressure + progress)"]:::data
        B1ASSEMBLY["Receive Assembly + Download\n(Blob + link)"]:::data
      end

      subgraph B2["Browser B"]
        direction TB
        B2UI["UI Shell\n(index.html + styles.css)"]:::frontend
        B2APP["Client App Logic\n(peer list + WebRTC + file send/receive)\n(client.js)"]:::frontend
        B2SIO["Socket.IO Client\n(signaling channel)"]:::control
        B2WRTC["WebRTC Stack\n(RTCPeerConnection + ICE + DataChannel)"]:::data
        B2PIPE["File API + Chunking Pipeline\n(buffering/backpressure + progress)"]:::data
        B2ASSEMBLY["Receive Assembly + Download\n(Blob + link)"]:::data
      end
    end

    subgraph SERVER["Server-side (single node) — Render.com Web Service"]
      direction TB
      NODE["Node.js Runtime"]:::server
      EXPRESS["Express Static Server\n(serves public/)"]:::http
      SIOSRV["Socket.IO Signaling Server\n(rooms/presence + offer/answer/ICE relay)\nNo server persistence"]:::control
      MEM["In-memory Peer Registry / Sessions\n(conceptual)"]:::serverInternal
    end

    STUN2["STUN server(s)\n(External)"]:::external
    TURN2["TURN relay (optional)\n(Not configured)"]:::externalOptional

    %% Internal wiring (client)
    B1UI -->|"drives"| B1APP
    B2UI -->|"drives"| B2APP

    B1APP -->|"uses"| B1SIO
    B2APP -->|"uses"| B2SIO

    B1APP -->|"creates"| B1WRTC
    B2APP -->|"creates"| B2WRTC

    B1APP -->|"streams"| B1PIPE
    B2APP -->|"streams"| B2PIPE

    B1PIPE -->|"send/recvvia"| B1WRTC
    B2PIPE -->|"send/recvvia"| B2WRTC

    B1WRTC -->|"deliversreceivedchunks"| B1ASSEMBLY
    B2WRTC -->|"deliversreceivedchunks"| B2ASSEMBLY

    %% Server internal wiring
    NODE --> EXPRESS
    NODE --> SIOSRV
    SIOSRV -->|"trackspeers"| MEM

    %% Network flows
    B1UI -->|"HTTP:GET staticassets"| EXPRESS
    B2UI -->|"HTTP:GET staticassets"| EXPRESS

    B1SIO <-->|"WebSocket:presence+SDP+ICE"| SIOSRV
    B2SIO <-->|"WebSocket:presence+SDP+ICE"| SIOSRV

    %% Critical: P2P data plane (bypasses server)
    B1WRTC <-->|"WebRTCDataChannel:encryptedfilechunks\n(progressacks/backpressure)"| B2WRTC

    %% ICE / NAT traversal
    B1WRTC <-->|"ICE/STUN"| STUN2
    B2WRTC <-->|"ICE/STUN"| STUN2

    B1WRTC -.->|"Optional:TURNrelay"| TURN2
    B2WRTC -.->|"Optional:TURNrelay"| TURN2
  end

  %% =========================
  %% Diagram 3 — Sequence
  %% =========================
  subgraph D3["Diagram 3 — Sequence (Connect + Signal + Transfer)"]
    direction TB

    A3["Browser A\n(client.js)"]:::frontend
    SRV3["Signaling Server\n(server.js + Socket.IO)"]:::control
    B3["Browser B\n(client.js)"]:::frontend
    STUN3["STUN server(s)"]:::external

    A3 -->|"1.connectSocketIO()"| SRV3
    B3 -->|"2.connectSocketIO()"| SRV3

    A3 -->|"3.joinRoom/setName"| SRV3
    SRV3 -->|"4.peerListUpdate"| B3
    SRV3 -->|"5.peerListUpdate"| A3

    A3 -->|"6.createOffer() + setLocalDescription"| A3
    A3 -->|"7.SocketIO:sendOffer"| SRV3
    SRV3 -->|"8.forwardOffer"| B3

    B3 -->|"9.setRemoteDescription(offer)"| B3
    B3 -->|"10.createAnswer() + setLocalDescription"| B3
    B3 -->|"11.SocketIO:sendAnswer"| SRV3
    SRV3 -->|"12.forwardAnswer"| A3

    A3 -->|"13.setRemoteDescription(answer)"| A3

    A3 -->|"14.onicecandidate:sendCandidate"| SRV3
    SRV3 -->|"15.forwardCandidate"| B3
    B3 -->|"16.onicecandidate:sendCandidate"| SRV3
    SRV3 -->|"17.forwardCandidate"| A3

    A3 <-->|"18.ICEchecks/STUNbindings"| STUN3
    B3 <-->|"19.ICEchecks/STUNbindings"| STUN3

    A3 <-->|"20.DataChannel opens (DTLS/SRTP)"| B3

    A3 -->|"21.sendMetadata(name,size,chunkPlan)"| B3
    A3 -->|"22.streamChunks() (chunking+backpressure)"| B3
    B3 -->|"23.ack/progress/backpressureSignals"| A3
    B3 -->|"24.reassembleBlob() + exposeDownloadLink"| B3
  end

  %% =========================
  %% Click events (mapped components)
  %% =========================
  click SCTX "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/server.js"
  click EXPRESS "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/server.js"
  click SIOSRV "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/server.js"
  click MEM "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/server.js"

  click B1UI "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/index.html"
  click B2UI "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/index.html"
  click B1APP "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/client.js"
  click B2APP "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/client.js"
  click B1SIO "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/client.js"
  click B2SIO "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/client.js"
  click B1WRTC "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/client.js"
  click B2WRTC "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/client.js"
  click B1PIPE "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/client.js"
  click B2PIPE "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/client.js"
  click B1ASSEMBLY "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/client.js"
  click B2ASSEMBLY "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/client.js"

  click A3 "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/client.js"
  click B3 "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/client.js"
  click SRV3 "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/server.js"

  click NODE "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/package.json"
  click SERVER "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/server.js"
  click CLIENTS "https://github.com/ameya-jarvis-07/p2p-file-sharing/tree/main/public/"
  click D1 "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/README.md"
  click D2 "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/package.json"
  click D3 "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/client.js"

  %% Direct mappings for UI assets
  click B1UI "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/styles.css"
  click B2UI "https://github.com/ameya-jarvis-07/p2p-file-sharing/blob/main/public/styles.css"

  %% =========================
  %% Styles (color-coded planes)
  %% =========================
  classDef user fill:#ffffff,stroke:#374151,stroke-width:1.2,color:#111827
  classDef server fill:#fee2e2,stroke:#991b1b,stroke-width:1.5,color:#7f1d1d
  classDef serverInternal fill:#fecaca,stroke:#991b1b,stroke-width:1.2,color:#7f1d1d

  classDef frontend fill:#e0f2fe,stroke:#075985,stroke-width:1.5,color:#0c4a6e

  %% Planes
  classDef http fill:#e5e7eb,stroke:#374151,stroke-width:1.3,color:#111827
  classDef control fill:#dbeafe,stroke:#1d4ed8,stroke-width:1.6,color:#1e3a8a
  classDef data fill:#dcfce7,stroke:#166534,stroke-width:1.6,color:#14532d

  classDef external fill:#f3f4f6,stroke:#4b5563,stroke-width:1.2,color:#111827
  classDef externalOptional fill:#f9fafb,stroke:#6b7280,stroke-width:1.2,color:#111827,stroke-dasharray: 5 5
```
