How 10billionpercent/virtual-cosmos works
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: App.jsx App shell · Cosmos.jsx Cosmos · index.js World state · socket.js Socket client
Components 13
Frontend
React + Vite client for identity setup, socket connection, and world rendering
- Entry · Bootstraps the frontend app
frontend/src/main.jsx - App shell · Top-level client state that routes between signup and live world
frontend/src/App.jsx - Signup · Collects nickname, avatar, and color for a tab-scoped player identity
frontend/src/components/Signup.jsx - Socket client · Shared browser connection used for realtime events
frontend/src/socket.js - Cosmos · Renders the live 2D world and consumes server state
frontend/src/components/Cosmos.jsx - Marker · Draws an individual player marker with visual effects
frontend/src/components/Marker.jsx - Chat · Handles local neighborhood chat interactions
frontend/src/components/Chat.jsx
Backend
Express + Socket.io server that hosts the authoritative shared world
- Server · Starts the HTTP and Socket.io server
backend/index.js - Connections · Accepts player joins and manages socket lifecycle
backend/index.js - World state · Keeps the shared world positions and runtime player state in memory
backend/index.js - Movement · Processes player movement and broadcasts updated positions
backend/index.js - Rules · Enforces spawn placement and collision-aware multiplayer constraints
backend/index.js - Chat relay · Routes proximity-based chat through the realtime server
backend/index.js
Connections 17
- 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