# aditya-gore22/chatapplication architecture

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

- Interactive diagram: https://gitdiagram.com/aditya-gore22/chatapplication
- Repository: https://github.com/aditya-gore22/chatapplication

## Overview

1. This repository is a two-tier P2P chat application: a small Node.js backend for signaling/presence, and a React/Vite frontend that owns the actual chat, media, and UI logic.

2. The backend is intentionally narrow. `backend/server.js` runs an Express + Socket.IO server on a configurable port and acts as the signaling relay between browsers. It handles user presence and WebRTC negotiation messages, but not the chat payload itself.

3. The frontend is the main application. `frontend/src/main.jsx` bootstraps the React app, and `App.jsx`/`App.css` provide the top-level shell for authentication, conversation selection, chat display, and call UI.

4. Global application state is split into two contexts:
   - `SocketContext.jsx` for the live Socket.IO connection and related connection state
   - `ChatContext.jsx` for selected peer, message state, and chat-related UI/data
   This separation suggests a clear boundary between transport/session state and conversation state.

5. WebRTC is the core runtime path for messages and calls. `hooks/usePeer.js` centralizes SimplePeer setup, SDP negotiation, ICE handling, and DataChannel lifecycle. The README makes clear that once peers connect, text/files/audio/video travel directly browser-to-browser without passing through the backend.

6. Presence and signaling flow through the backend before P2P connection. Users log in, announce themselves online over Socket.IO, choose a recipient from the sidebar, and then the backend brokers offer/answer/candidate exchange until the peer connection is established.

7. The UI is componentized by function:
   - `Login` for username entry
   - `Sidebar` for online users / peer selection
   - `ChatArea` for the conversation and message composer
   - `MessageBubble` for rendering individual messages
   - `VideoCall` for audio/video call controls and the minimized call window
   - `Notifications` for toast-style alerts
   This maps well to a diagram showing a central app shell with several feature panels.

8. Media and device access are isolated in hooks. `useMediaDevices.js` likely manages camera/mic selection and permissions, while `usePeer.js` ties those tracks into the WebRTC session. This means the app has a distinct “media acquisition” boundary separate from “signaling” and “rendering.”

9. Utility code reflects cross-cutting concerns rather than feature modules:
   - `sanitize.js` for XSS-safe rendering of incoming messages
   - `fileUtils.js` for chunking/processing file transfers
   - `soundUtils.js` for notification or call sounds
   These utilities support secure message display and richer transport types.

10. Feature behavior matches the architecture: usernames are stored locally, online status is socket-based, typing indicators are transient signaling events, and file transfer uses chunked DataChannel payloads. This makes the frontend responsible for session UX while the backend remains a relay.

11. The deployment boundary is also explicit. The frontend talks to the backend via `VITE_SERVER_URL`, while the backend allows a configurable `CLIENT_URL` for CORS. In production, HTTPS is required because WebRTC and camera/mic access depend on secure origins.

12. For an architecture diagram, the most important relationship is: browser UI/state → Socket.IO signaling server → browser peer connection. All chat content, attachments, and calls ultimately flow through the WebRTC DataChannel and media streams, not through the server.

Main entry points: [`frontend/src/App.jsx`](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/App.jsx) (App shell), [`frontend/src/hooks/usePeer.js`](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/hooks/usePeer.js) (Peer engine), [`backend/server.js`](https://github.com/aditya-gore22/chatapplication/blob/master/backend/server.js) (Server), [`frontend/src/components/ChatArea/ChatArea.jsx`](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/components/ChatArea/ChatArea.jsx) (Chat area)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Backend"]
  node_backend_server["Server<br/>Express/Socket.IO relay<br/>[server.js]"]
end

subgraph group_frontend["Frontend"]
  node_main["Bootstrap<br/>React entry<br/>[main.jsx]"]
  node_app_shell["App shell<br/>UI composition<br/>[App.jsx]"]
  node_app_styles["App styles<br/>global CSS<br/>[App.css]"]
  node_login["Login<br/>auth UI<br/>[Login.jsx]"]
  node_sidebar["Sidebar<br/>presence UI<br/>[Sidebar.jsx]"]
  node_chat_area["Chat area<br/>conversation UI<br/>[ChatArea.jsx]"]
  node_message_bubble["Message bubble<br/>message renderer<br/>[MessageBubble.jsx]"]
  node_video_call["Video call<br/>call UI<br/>[VideoCall.jsx]"]
  node_toast["Toast<br/>alert UI"]
end

subgraph group_shared["Shared runtime"]
  node_socket_ctx(("Socket state<br/>React context<br/>[SocketContext.jsx]"))
  node_chat_ctx(("Chat state<br/>React context<br/>[ChatContext.jsx]"))
  node_peer_hook{{"Peer engine<br/>WebRTC hook<br/>[usePeer.js]"}}
  node_media_hook{{"Media devices<br/>device hook<br/>[useMediaDevices.js]"}}
  node_notify_hook{{"Notifications<br/>notification hook<br/>[useNotification.js]"}}
  node_sanitize_util["Sanitize<br/>security util<br/>[sanitize.js]"]
  node_file_util["File utils<br/>transfer util<br/>[fileUtils.js]"]
  node_sound_util["Sound utils<br/>media util<br/>[soundUtils.js]"]
end

node_main -->|"mounts"| node_app_shell
node_app_shell -->|"uses"| node_socket_ctx
node_app_shell -->|"uses"| node_chat_ctx
node_app_shell -->|"shows"| node_login
node_app_shell -->|"shows"| node_sidebar
node_app_shell -->|"shows"| node_chat_area
node_app_shell -->|"shows"| node_video_call
node_app_shell -->|"shows"| node_toast
node_sidebar -->|"selects peer"| node_chat_ctx
node_chat_area -->|"renders"| node_message_bubble
node_chat_area -->|"sends data"| node_peer_hook
node_video_call -->|"captures media"| node_media_hook
node_video_call -->|"streams media"| node_peer_hook
node_peer_hook -->|"signals through"| node_socket_ctx
node_socket_ctx -->|"connects"| node_backend_server
node_backend_server -.->|"relays presence"| node_backend_server
node_backend_server -.->|"relays negotiation"| node_backend_server
node_peer_hook -->|"chunks files"| node_file_util
node_peer_hook -->|"guards text"| node_sanitize_util
node_notify_hook -->|"plays cues"| node_sound_util
node_app_shell -->|"coordinates alerts"| node_notify_hook
node_app_shell -->|"styles"| node_app_styles

click node_backend_server "https://github.com/aditya-gore22/chatapplication/blob/master/backend/server.js"
click node_main "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/main.jsx"
click node_app_shell "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/App.jsx"
click node_app_styles "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/App.css"
click node_socket_ctx "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/context/SocketContext.jsx"
click node_chat_ctx "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/context/ChatContext.jsx"
click node_peer_hook "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/hooks/usePeer.js"
click node_media_hook "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/hooks/useMediaDevices.js"
click node_notify_hook "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/hooks/useNotification.js"
click node_sanitize_util "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/utils/sanitize.js"
click node_file_util "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/utils/fileUtils.js"
click node_sound_util "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/utils/soundUtils.js"
click node_login "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/components/Login/Login.jsx"
click node_sidebar "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/components/Sidebar/Sidebar.jsx"
click node_chat_area "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/components/ChatArea/ChatArea.jsx"
click node_message_bubble "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/components/MessageBubble/MessageBubble.jsx"
click node_video_call "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/components/VideoCall/VideoCall.jsx"
click node_toast "https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/components/Notifications/NotificationToast.jsx"

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_backend_server toneBlue
class node_main,node_app_shell,node_app_styles,node_login,node_sidebar,node_chat_area,node_message_bubble,node_video_call,node_toast toneAmber
class node_socket_ctx,node_chat_ctx,node_peer_hook,node_media_hook,node_notify_hook,node_sanitize_util,node_file_util,node_sound_util toneMint
```

## Components

### Backend

Express and Socket.IO signaling relay.

- [Server](https://github.com/aditya-gore22/chatapplication/blob/master/backend/server.js): Express/Socket.IO relay · Runs the signaling server, presence tracking, and WebRTC negotiation relay. · `backend/server.js`

### Frontend

React app with chat, media, and peer-to-peer runtime.

- [Bootstrap](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/main.jsx): React entry · Mounts the React application into the page. · `frontend/src/main.jsx`
- [App shell](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/App.jsx): UI composition · Top-level UI for authentication, conversation selection, chat, and call flow. · `frontend/src/App.jsx`
- [App styles](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/App.css): global CSS · Application-wide layout and visual styling. · `frontend/src/App.css`
- [Login](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/components/Login/Login.jsx): auth UI · Collects username and starts a local session. · `frontend/src/components/Login/Login.jsx`
- [Sidebar](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/components/Sidebar/Sidebar.jsx): presence UI · Shows online users and drives peer selection. · `frontend/src/components/Sidebar/Sidebar.jsx`
- [Chat area](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/components/ChatArea/ChatArea.jsx): conversation UI · Renders the active conversation and message composer. · `frontend/src/components/ChatArea/ChatArea.jsx`
- [Message bubble](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/components/MessageBubble/MessageBubble.jsx): message renderer · Displays an individual chat message in the conversation. · `frontend/src/components/MessageBubble/MessageBubble.jsx`
- [Video call](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/components/VideoCall/VideoCall.jsx): call UI · Manages audio/video controls and the minimized call window. · `frontend/src/components/VideoCall/VideoCall.jsx`
- [Toast](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/components/Notifications/NotificationToast.jsx): alert UI · Renders transient notification messages. · `frontend/src/components/Notifications/NotificationToast.jsx`

### Shared runtime

Cross-cutting state, hooks, and utilities used by the app.

- [Socket state](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/context/SocketContext.jsx): React context · Holds the live Socket.IO connection and connection/session state. · `frontend/src/context/SocketContext.jsx`
- [Chat state](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/context/ChatContext.jsx): React context · Tracks selected peer, messages, and chat-related UI state. · `frontend/src/context/ChatContext.jsx`
- [Peer engine](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/hooks/usePeer.js): WebRTC hook · Sets up SimplePeer, SDP negotiation, ICE, and DataChannel lifecycle. · `frontend/src/hooks/usePeer.js`
- [Media devices](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/hooks/useMediaDevices.js): device hook · Handles camera and microphone access and device selection. · `frontend/src/hooks/useMediaDevices.js`
- [Notifications](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/hooks/useNotification.js): notification hook · Coordinates transient alerts and sound feedback. · `frontend/src/hooks/useNotification.js`
- [Sanitize](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/utils/sanitize.js): security util · Cleans incoming message content for safe rendering. · `frontend/src/utils/sanitize.js`
- [File utils](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/utils/fileUtils.js): transfer util · Supports file chunking and processing for DataChannel transfers. · `frontend/src/utils/fileUtils.js`
- [Sound utils](https://github.com/aditya-gore22/chatapplication/blob/master/frontend/src/utils/soundUtils.js): media util · Provides audio cues for notifications and call events. · `frontend/src/utils/soundUtils.js`

## Connections

- Bootstrap → App shell: mounts · Bootstraps the application shell.
- App shell → Socket state: uses · Consumes live signaling and connection state.
- App shell → Chat state: uses · Consumes conversation state and selected peer.
- App shell → Login: shows · Presents authentication when the session is not ready.
- App shell → Sidebar: shows · Displays presence and peer selection.
- App shell → Chat area: shows · Displays the active conversation.
- App shell → Video call: shows · Surfaces call controls and call window.
- App shell → Toast: shows · Surfaces transient alerts.
- Sidebar → Chat state: selects peer · Changes the active conversation target.
- Chat area → Message bubble: renders · Uses message bubbles for conversation history.
- Chat area → Peer engine: sends data · Uses peer connection for direct chat/file transport.
- Video call → Media devices: captures media · Uses local camera and microphone tracks.
- Video call → Peer engine: streams media · Attaches media tracks to the peer session.
- Peer engine → Socket state: signals through · Relays offers, answers, and ICE candidates over Socket.IO.
- Socket state → Server: connects · Establishes the Socket.IO session to the signaling server.
- Server → Server: relays presence · Broadcasts online state and user availability.
- Server → Server: relays negotiation · Forwards WebRTC signaling messages between peers.
- Peer engine → File utils: chunks files · Supports file transfer over the DataChannel.
- Peer engine → Sanitize: guards text · Helps keep incoming content safe before display.
- Notifications → Sound utils: plays cues · Uses audio feedback for alerts and calls.
- App shell → Notifications: coordinates alerts · Triggers UI notifications and sound cues.
- App shell → App styles: styles · Applies global shell styling.
