A battle-tested reference architecture demonstrating how to decouple continuous, high-frequency physical simulation engines (
-
$87.1%$ Bandwidth Reduction: Cut dynamic streaming volume from$14.42\text{ MB}$ down to$1.87\text{ MB}$ over 5s ($2,954\text{ KB/s} \to 382\text{ KB/s}$ );$> 99.5%$ reduction during stationary hold ($12.4\text{ KB/s}$ ). -
$15\times$ Faster V8 Deserialization: Reduced client JSON parsing latency from$231.4\ \mu\text{s}$ to$15.5\ \mu\text{s}$ per broadcast packet. -
Rock-Solid 60 FPS WebGL Rendering: Slashed telemetry frame-budget consumption from
$1.39%$ to$0.09%$ of the$16.67\text{ ms}$ render window, freeing up$99.9%$ of the main thread for GPU draw calls and zero-allocation SLERP interpolation. -
Robotics Kinematic Fidelity: Modeled an articulated 10-assembly robot using a URDF-inspired hierarchical scene graph with standard robotics link/joint nomenclatures, adapted to Three.js's
$+Y$ -up coordinate convention with contiguous mechanical links. - Autonomous In-Browser Web Worker Demo: Implemented dual-mode telemetry ingestion. When the Node.js gateway is unreachable (e.g. on static GitHub Pages), a background Web Worker autonomously simulates URDF kinematics and deadband delta compression at 60 FPS without blocking the main WebGL thread, accompanied by a clear visual disclaimer.
-
Production Docker Topology: Orchestrated via Docker Compose with an unprivileged Node 22 Alpine gateway and an ultra-lightweight Nginx Alpine web server (
$< 30\text{ MB}$ image).
Test the interactive 3D digital twin directly in your browser without spinning up local servers or Docker containers: π https://leanoa.github.io/twinstream/
(Static deployment runs on an autonomous background Web Worker computing URDF forward kinematics and deadband delta compression at 60 FPS, completely freeing the main thread for GPU draw calls).
To launch the complete containerized stack with zero host dependencies:
docker compose up -d --build- Frontend Web Client: Open http://localhost:5173 in your browser.
- Telemetry Gateway: WebSocket streaming active on
ws://localhost:8080. - To stop:
docker compose down.
Prerequisites: Node.js 22+, npm.
# 1. Install dependencies
npm install
npm install --prefix client
# 2. Build client assets
npm run client:build
# 3. Launch Telemetry Gateway & Physics Simulator (Port 8080)
npm run gateway
# 4. In a separate terminal, launch Vite Development Server (Port 5173)
npm run clientFor static hosting (e.g., GitHub Pages) or portfolio walkthroughs without spinning up Docker or Node.js servers:
# Build static client bundle with relative asset paths (base: './')
npm run client:build
# Preview static bundle locally
npm run preview --prefix client- The client detects WebSocket unavailability within 1,500 ms and autonomously boots
telemetry_worker.tsin the background. - The 3D robot articulates at 60 FPS directly in the browser with live deadband delta compression metrics and an explicit visual disclaimer notice.
Run the headless benchmarking suite comparing Monolithic vs. Decoupled Delta streaming:
npm run benchmark
# To test stationary (quiescent) state:
npm run benchmark -- --quiescentExecute the full Vitest suite (51 automated tests across 7 test suites):
npm testflowchart TD
subgraph PhysicsLayer["1. Continuous Physics Layer (100 Hz)"]
Physics["PhysicsEngine\n(1,000 Channels, URDF Kinematics)"]
InternalBuffer["Pre-allocated Float64 Buffer\n(Zero V8 Allocation)"]
Physics --> InternalBuffer
end
subgraph GatewayLayer["2. Telemetry Gateway (60 Hz Paced)"]
Compressor["DeltaCompressor\n(Deadband Filtering: |Ξs| > Ξ΅)"]
WSGateway["WebSocket Server (ws)\n(Port 8080, Backpressure Guard)"]
InternalBuffer -->|IPC / Memory State| Compressor
Compressor --> WSGateway
end
subgraph ClientLayer["3. Three.js WebGL Client (60 FPS Render Loop)"]
Receiver["TelemetryReceiver\n(WebSocket Client)"]
RingBuffer["Target State Ring Buffer\n(Lerp / Slerp Interpolator)"]
SceneMgr["SceneManager\n(Contiguous URDF Meshes, Three.js)"]
Renderer["WebGLRenderer\n(requestAnimationFrame @ 60 FPS)"]
WSGateway -->|"Compact Delta JSON: [channel, value]"| Receiver
Receiver --> RingBuffer
RingBuffer --> SceneMgr
SceneMgr --> Renderer
end
-
Decoupled Frequencies: Physics simulates at
$100\text{ Hz}$ ($10\text{ ms}$ ) for numerical stability; Gateway downsamples to$60\text{ Hz}$ ($16.67\text{ ms}$ ) matching display refresh rates; WebGL client renders independently at$60\text{ FPS}$ with Lerp/Slerp spatial smoothing. -
Deadband Compression (
$\epsilon = 0.001$ ): Filters invariant channels; transmits only signals where$|s_i(t) - s_i(t_{\text{last}})| > \epsilon$ . -
Zero-Allocation Render Loop: Uses pre-allocated math scratchpads (
_scratchPos,_scratchQuat) to completely eliminate heap allocation during animation frames, eliminating V8 Garbage Collection Stop-the-World pauses. - Autonomous Web Worker Fallback: Features dual-mode ingestion. If WebSocket connection to the gateway is unavailable or times out, the client automatically spins up a background Web Worker running forward kinematics and deadband compression without blocking the main WebGL thread.
| Metric | Monolithic (Full-State Baseline) | Decoupled (Deadband |
Improvement |
|---|---|---|---|
| Total Transmitted Volume | |||
| Average Packet Size | |||
| Network Throughput | |||
| V8 JSON Parse Latency (avg) | |||
| 60 FPS Frame Budget Consumed | |||
| Quiescent (Hold) Throughput | |||
| Quiescent Parse Latency |
.
βββ .dockerignore # Docker context exclusion rules
βββ Dockerfile.gateway # Multi-stage Node 22 Alpine build for gateway
βββ Dockerfile.client # Multi-stage Vite build + Nginx Alpine runner
βββ docker-compose.yml # Multi-service topology (gateway: 8080, client: 5173)
βββ package.json # Root scripts, Vitest runner, dependencies
βββ tsconfig.json # TypeScript compiler configuration
βββ schema/ # JSON Schema data contracts
β βββ full_state.schema.json # 1,000-channel full snapshot contract
β βββ delta_packet.schema.json # Compact tuple delta packet contract
βββ data/ # Static layout definitions
β βββ mock_telemetry_layout.json # 1,000-channel metadata & deadbands
βββ simulator/ # 100 Hz continuous physics engine
β βββ physics_engine.ts # Kinematics solver & signal generator
βββ gateway/ # High-frequency telemetry gateway
β βββ delta_compressor.ts # Deadband delta compression engine
β βββ telemetry_server.ts # WebSocket broadcast server with backpressure
β βββ index.ts # Gateway executable entry point
βββ client/ # Three.js WebGL frontend (Vite + TypeScript)
β βββ nginx.conf # Nginx SPA fallback, gzip & cache headers
β βββ src/scene_manager.ts # Contiguous URDF kinematic meshes & lighting
β βββ src/telemetry_receiver.ts # Zero-allocation Lerp/Slerp interpolator & dual-mode fallback
β βββ src/hud_manager.ts # Diagnostic HUD & demo disclaimer manager
β βββ src/workers/
β β βββ telemetry_worker.ts # Autonomous in-browser Web Worker kinematics & deadband engine
β βββ src/main.ts # App lifecycle & stats.js monitor
βββ benchmarks/ # Empirical performance profiling
β βββ run_telemetry_benchmark.ts # Headless comparative benchmark engine
βββ tests/ # Automated Vitest test suite (51 tests)
β βββ test_contracts.test.ts # Schema validation & type assertions
β βββ test_simulator.test.ts # 100 Hz timer precision & kinematic bounds
β βββ test_gateway.test.ts # WebSocket multi-client & deadband tests
β βββ test_client_interpolator.test.ts # Lerp/Slerp interpolation precision
β βββ test_demo_worker.test.ts # Autonomous Web Worker kinematics & fallback tests
β βββ test_docker_topology.test.ts # Multi-stage Docker & Compose verification
β βββ test_benchmark.test.ts # Headless benchmark thresholds & metrics
βββ docs/ # In-depth technical documentation
β βββ README.md # Technical documentation hub index
β βββ ARCHITECTURE_DECISIONS.md # Architecture Decision Records (ADRs 01-07)
β βββ GLOSSARY_AND_CONCEPTS.md # Abbreviations, standards & mathematical theory
β βββ LESSONS_LEARNED.md # Hard-learned production engineering lessons
βββ ARTICLE_DRAFT.md # Publication-grade technical case study
- π Documentation Hub: Central index to all engineering references.
- ποΈ Architectural Decision Records (ADRs): In-depth rationale for the three-tier architecture, deadband algorithms, quaternion kinematics, and zero-allocation patterns.
- π Glossary & Mathematical Concepts: Full theoretical derivations (Quaternion SLERP, Forward Kinematics, V8 GC Mechanics).
- π‘ Hard-Learned Lessons: Practical gotchas (Alpine loopback IPv6 vs IPv4, antipodal quaternion spins, floating parts).
- π Publication-Ready Article Draft: Complete engineering case study ready for Medium or technical blogs.
MIT License. Designed and engineered for high-performance real-time telemetry systems.