# 10billionpercent/virtual-cosmos architecture

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

- Interactive diagram: https://gitdiagram.com/10billionpercent/virtual-cosmos
- Repository: https://github.com/10billionpercent/virtual-cosmos

## Overview

1. **Top-level split**
   - The repo is a classic two-process app: a **Node.js/Express + Socket.io backend** in `backend/` and a **Vite + React frontend** in `frontend/`.
   - The architecture is centered on a **real-time shared world** rather than request/response pages.

2. **Backend boundary**
   - `backend/index.js` is the server entry point.
   - Its main role is to host the HTTP/socket server, accept player connections, coordinate movement/state sync, and enforce multiplayer rules such as spawn placement and collision handling.
   - There is no database directory or persistence layer in the tree, so the world likely lives in **server memory** during runtime.

3. **Frontend application shell**
   - `frontend/src/main.jsx` boots the React app.
   - `frontend/src/App.jsx` is the top-level state switch: it likely decides whether the user is still in **signup/identity setup** or already inside the live cosmos.
   - The app is bundled with **Vite** and styled with plain CSS (`App.css`, `index.css`).

4. **Identity and session boundary**
   - `frontend/src/components/Signup.jsx` handles creation of the explorer identity: nickname, avatar, and color.
   - The README says identities are tab-isolated via **sessionStorage**, so each browser tab can hold a separate player profile without overwriting another tab.
   - This is an important architectural boundary: identity is **client-scoped**, not globally shared.

5. **Real-time connection layer**
   - `frontend/src/socket.js` is the client’s Socket.io integration point.
   - This file likely creates and exports the shared socket connection used by UI components to send movement, join, and chat events.
   - It separates transport concerns from the visual components, which keeps the React tree focused on rendering and interaction.

6. **World rendering subsystem**
   - `frontend/src/components/Cosmos.jsx` is the main scene renderer.
   - Based on the README, this is where the 2D world is drawn, likely using **Pixi.js** for high-performance canvas/WebGL rendering.
   - This component is the visual consumer of server-sent player positions and proximity data.

7. **Player/avatar presentation**
   - `frontend/src/components/Marker.jsx` appears to render an individual player marker/avatar in the world.
   - It likely combines avatar image, signature color, and visual effects such as the “diffused glow” described in the README.
   - This is the per-entity visual unit inside the larger cosmos scene.

8. **Chat subsystem**
   - `frontend/src/components/Chat.jsx` handles local neighborhood chat UI.
   - The README indicates chat is **proximity-based** and Socket.io-backed, so messages are probably only enabled or routed when nearby users are within range.
   - This creates a distinct interaction boundary: global world movement vs. local social communication.

9. **Movement and proximity data flow**
   - The key runtime loop is: user input → client emits movement → backend updates positions/collisions → backend broadcasts state → frontend re-renders markers and proximity effects.
   - Chat availability also depends on this spatial state, so the server’s position model directly controls UI behavior.

10. **Collision and spawn logic**
   - The README calls out **server-side-aware collision resolution** and clear spawn placement.
   - That means the backend is authoritative for avoiding overlaps and deciding where a new player can appear, rather than trusting the client.
   - This is a core multiplayer integrity boundary.

11. **Join/leave animation flow**
   - The frontend likely animates players entering or leaving the scene with alpha transitions, as noted in the README.
   - These are presentation-layer concerns driven by server connection events, not separate gameplay state.

12. **Infrastructure and tooling**
   - Backend dev workflow uses **Node.js, Express, Socket.io, and Nodemon**.
   - Frontend uses **React, Pixi.js, Socket.io-client, and Vite**.
   - There is no evidence of external services such as auth providers, databases, or cloud storage; the app appears self-contained and local-development oriented.

13. **Diagram-friendly subsystem boundaries**
   - A useful architecture diagram would show:
     - **Signup/session identity** on the client
     - **Socket transport** between client and server
     - **Authoritative world state** on the backend
     - **2D scene rendering** in the frontend
     - **Proximity chat** as a separate real-time interaction path
   - The main interaction is not HTTP page loading, but **continuous bidirectional socket synchronization**.

Main entry points: [`frontend/src/App.jsx`](https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/App.jsx) (App shell), [`frontend/src/components/Cosmos.jsx`](https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/components/Cosmos.jsx) (Cosmos), [`backend/index.js`](https://github.com/10billionpercent/virtual-cosmos/blob/main/backend/index.js) (World state), [`frontend/src/socket.js`](https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/socket.js) (Socket client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_fe_main(("Entry<br/>React boot<br/>[main.jsx]"))
  node_fe_app["App shell<br/>State switch<br/>[App.jsx]"]
  node_fe_signup["Signup<br/>Identity form<br/>[Signup.jsx]"]
  node_fe_socket(("Socket client<br/>Socket.io client<br/>[socket.js]"))
  node_fe_cosmos["Cosmos<br/>Scene renderer<br/>[Cosmos.jsx]"]
  node_fe_marker(("Marker<br/>Avatar view<br/>[Marker.jsx]"))
  node_fe_chat["Chat<br/>Proximity UI<br/>[Chat.jsx]"]
end

subgraph group_backend["Backend"]
  node_be_entry(("Server<br/>Express entry<br/>[index.js]"))
  node_be_connect["Connections<br/>Realtime session<br/>[index.js]"]
  node_be_world[("World state<br/>Authoritative state<br/>[index.js]")]
  node_be_move["Movement<br/>Sync logic<br/>[index.js]"]
  node_be_rules{{"Rules<br/>Collision/spawn<br/>[index.js]"}}
  node_be_chat["Chat relay<br/>Spatial chat<br/>[index.js]"]
end

node_fe_main -->|"mounts"| node_fe_app
node_fe_app -->|"identity gate"| node_fe_signup
node_fe_app -->|"enters world"| node_fe_cosmos
node_fe_app -->|"chat ui"| node_fe_chat
node_fe_signup -->|"profile join"| node_fe_socket
node_fe_cosmos -->|"renders"| node_fe_marker
node_fe_cosmos -->|"subscribes"| node_fe_socket
node_fe_chat -->|"sends chat"| node_fe_socket
node_fe_socket -->|"connects"| node_be_entry
node_be_entry -->|"accepts"| node_be_connect
node_be_connect -->|"updates"| node_be_world
node_be_world -->|"drives"| node_be_move
node_be_move -->|"checks"| node_be_rules
node_be_rules -->|"validates"| node_be_world
node_be_world -->|"proximity"| node_be_chat
node_be_chat -->|"delivers"| node_fe_chat
node_be_move -->|"broadcasts"| node_fe_cosmos

click node_fe_main "https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/main.jsx"
click node_fe_app "https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/App.jsx"
click node_fe_signup "https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/components/Signup.jsx"
click node_fe_socket "https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/socket.js"
click node_fe_cosmos "https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/components/Cosmos.jsx"
click node_fe_marker "https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/components/Marker.jsx"
click node_fe_chat "https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/components/Chat.jsx"
click node_be_entry "https://github.com/10billionpercent/virtual-cosmos/blob/main/backend/index.js"
click node_be_connect "https://github.com/10billionpercent/virtual-cosmos/blob/main/backend/index.js"
click node_be_world "https://github.com/10billionpercent/virtual-cosmos/blob/main/backend/index.js"
click node_be_move "https://github.com/10billionpercent/virtual-cosmos/blob/main/backend/index.js"
click node_be_rules "https://github.com/10billionpercent/virtual-cosmos/blob/main/backend/index.js"
click node_be_chat "https://github.com/10billionpercent/virtual-cosmos/blob/main/backend/index.js"

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_fe_main,node_fe_app,node_fe_signup,node_fe_socket,node_fe_cosmos,node_fe_marker,node_fe_chat toneBlue
class node_be_entry,node_be_connect,node_be_world,node_be_move,node_be_rules,node_be_chat toneAmber
```

## Components

### Frontend

React + Vite client for identity setup, socket connection, and world rendering

- [Entry](https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/main.jsx): React boot · Bootstraps the frontend app · `frontend/src/main.jsx`
- [App shell](https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/App.jsx): State switch · Top-level client state that routes between signup and live world · `frontend/src/App.jsx`
- [Signup](https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/components/Signup.jsx): Identity form · Collects nickname, avatar, and color for a tab-scoped player identity · `frontend/src/components/Signup.jsx`
- [Socket client](https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/socket.js): Socket.io client · Shared browser connection used for realtime events · `frontend/src/socket.js`
- [Cosmos](https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/components/Cosmos.jsx): Scene renderer · Renders the live 2D world and consumes server state · `frontend/src/components/Cosmos.jsx`
- [Marker](https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/components/Marker.jsx): Avatar view · Draws an individual player marker with visual effects · `frontend/src/components/Marker.jsx`
- [Chat](https://github.com/10billionpercent/virtual-cosmos/blob/main/frontend/src/components/Chat.jsx): Proximity UI · Handles local neighborhood chat interactions · `frontend/src/components/Chat.jsx`

### Backend

Express + Socket.io server that hosts the authoritative shared world

- [Server](https://github.com/10billionpercent/virtual-cosmos/blob/main/backend/index.js): Express entry · Starts the HTTP and Socket.io server · `backend/index.js`
- [Connections](https://github.com/10billionpercent/virtual-cosmos/blob/main/backend/index.js): Realtime session · Accepts player joins and manages socket lifecycle · `backend/index.js`
- [World state](https://github.com/10billionpercent/virtual-cosmos/blob/main/backend/index.js): Authoritative state · Keeps the shared world positions and runtime player state in memory · `backend/index.js`
- [Movement](https://github.com/10billionpercent/virtual-cosmos/blob/main/backend/index.js): Sync logic · Processes player movement and broadcasts updated positions · `backend/index.js`
- [Rules](https://github.com/10billionpercent/virtual-cosmos/blob/main/backend/index.js): Collision/spawn · Enforces spawn placement and collision-aware multiplayer constraints · `backend/index.js`
- [Chat relay](https://github.com/10billionpercent/virtual-cosmos/blob/main/backend/index.js): Spatial chat · Routes proximity-based chat through the realtime server · `backend/index.js`

## Connections

- Entry → App shell: mounts · React entry mounts the application shell
- App shell → Signup: identity gate · App shows signup until a player identity exists
- App shell → Cosmos: enters world · App switches into the live cosmos after identity setup
- App shell → Chat: chat ui · App coordinates the chat panel in the live state
- Signup → Socket client: profile join · Signup initializes the client session and join flow
- Cosmos → Marker: renders · Scene renders each player marker from shared state
- Cosmos → Socket client: subscribes · Cosmos consumes realtime world updates from the socket client
- Chat → Socket client: sends chat · Chat UI emits and receives proximity messages through Socket.io
- Socket client → Server: connects · Browser client connects to the realtime server
- Server → Connections: accepts · Server entry hosts socket sessions
- Connections → World state: updates · Connections update the in-memory world model
- World state → Movement: drives · World state is advanced by movement sync logic
- Movement → Rules: checks · Movement resolution consults spawn and collision rules
- Rules → World state: validates · Rules write back authoritative placement into the shared world
- World state → Chat relay: proximity · Spatial state determines which chat peers can interact
- Chat relay → Chat: delivers · Server relays nearby chat to the client UI
- Movement → Cosmos: broadcasts · Server sends updated positions for scene re-rendering
