# hhaider3/motionlab architecture

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

- Interactive diagram: https://gitdiagram.com/hhaider3/motionlab
- Repository: https://github.com/hhaider3/motionlab

## Overview

Motion Lab pairs a phone controller with a desktop sword game using a session ID encoded in a QR link. The phone captures motion/orientation and streams packets to a relay over WebSocket, with HTTP fallback; the desktop receives sensor events and uses them to drive the game and scoring. Hits travel back through the relay as phone vibration feedback. The relay keeps session state in memory. The README documents these capabilities; some frontend wiring is in unsampled portions of MotionLab.jsx, so those links are cited to the README.

Main entry points: [`src/components/MotionLab.jsx`](https://github.com/hhaider3/motionlab/blob/main/src/components/MotionLab.jsx) (Phone sensor client), [`server/motion-relay-server.mjs`](https://github.com/hhaider3/motionlab/blob/main/server/motion-relay-server.mjs) (Relay endpoints), [`server/websocket-relay.mjs`](https://github.com/hhaider3/motionlab/blob/main/server/websocket-relay.mjs) (WebSocket framing), [`src/App.jsx`](https://github.com/hhaider3/motionlab/blob/main/src/App.jsx) (Route selector)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Browser experience"]
  node_app_router["Route selector<br/>[App.jsx]"]
  node_pairing["Desktop pairing<br/>[MotionLab.jsx]"]
  node_qr["QR generation<br/>[qrCode.js]"]
  node_phone_client["Phone sensor client<br/>[MotionLab.jsx]"]
  node_telemetry["Live telemetry<br/>[MotionLab.jsx]"]
  node_sword_game["Sword game scene<br/>[MotionLab.jsx]"]
  node_scoring["Hit scoring<br/>[MotionLab.jsx]"]
  node_vibration["Phone vibration<br/>[MotionLab.jsx]"]
end

subgraph group_relay["Motion relay"]
  node_relay_http["Relay endpoints"]
  node_session_state[("Session state")]
  node_sensor_dispatch["Sensor dispatch"]
  node_feedback_dispatch["Feedback dispatch"]
end

subgraph group_protocol["Relay protocol"]
  node_websocket["WebSocket framing"]
end

node_phone_user(("Phone user"))
node_desktop_user(("Desktop player"))

node_desktop_user -->|"opens app"| node_app_router
node_app_router -->|"routes desktop"| node_pairing
node_app_router -->|"routes phone"| node_phone_client
node_pairing -->|"generates code"| node_qr
node_phone_user -->|"starts sensors"| node_phone_client
node_pairing -->|"pairs session"| node_phone_client
node_phone_client -->|"publishes packets"| node_relay_http
node_phone_client -->|"streams packets"| node_websocket
node_relay_http -->|"updates sessions"| node_session_state
node_relay_http -->|"routes sensor requests"| node_sensor_dispatch
node_sensor_dispatch -->|"stores latest packet"| node_session_state
node_sensor_dispatch -->|"streams sensor events"| node_telemetry
node_telemetry -->|"drives sword"| node_sword_game
node_sword_game -->|"detects cuts"| node_scoring
node_scoring -->|"posts hit feedback"| node_relay_http
node_relay_http -->|"routes feedback"| node_feedback_dispatch
node_feedback_dispatch -->|"sends feedback"| node_websocket
node_websocket -->|"delivers feedback"| node_phone_client
node_phone_client -->|"vibrates on hit"| node_vibration
node_phone_user -->|"feels cut feedback"| node_vibration
node_relay_http -->|"uses protocol helpers"| node_websocket
node_desktop_user -->|"scans QR"| node_pairing

click node_app_router "https://github.com/hhaider3/motionlab/blob/main/src/App.jsx"
click node_pairing "https://github.com/hhaider3/motionlab/blob/main/src/components/MotionLab.jsx"
click node_qr "https://github.com/hhaider3/motionlab/blob/main/src/utils/qrCode.js"
click node_phone_client "https://github.com/hhaider3/motionlab/blob/main/src/components/MotionLab.jsx"
click node_telemetry "https://github.com/hhaider3/motionlab/blob/main/src/components/MotionLab.jsx"
click node_sword_game "https://github.com/hhaider3/motionlab/blob/main/src/components/MotionLab.jsx"
click node_scoring "https://github.com/hhaider3/motionlab/blob/main/src/components/MotionLab.jsx"
click node_relay_http "https://github.com/hhaider3/motionlab/blob/main/server/motion-relay-server.mjs"
click node_session_state "https://github.com/hhaider3/motionlab/blob/main/server/motion-relay-server.mjs"
click node_sensor_dispatch "https://github.com/hhaider3/motionlab/blob/main/server/motion-relay-server.mjs"
click node_feedback_dispatch "https://github.com/hhaider3/motionlab/blob/main/server/motion-relay-server.mjs"
click node_websocket "https://github.com/hhaider3/motionlab/blob/main/server/websocket-relay.mjs"
click node_vibration "https://github.com/hhaider3/motionlab/blob/main/src/components/MotionLab.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_app_router,node_pairing,node_qr,node_phone_client,node_telemetry,node_sword_game,node_scoring,node_vibration,node_phone_user toneBlue
class node_relay_http,node_session_state,node_sensor_dispatch,node_feedback_dispatch toneAmber
class node_websocket toneMint
class node_desktop_user toneIndigo
```

## Components

### Browser experience

- [Route selector](https://github.com/hhaider3/motionlab/blob/main/src/App.jsx): `src/App.jsx`
- [Desktop pairing](https://github.com/hhaider3/motionlab/blob/main/src/components/MotionLab.jsx): `src/components/MotionLab.jsx`
- [QR generation](https://github.com/hhaider3/motionlab/blob/main/src/utils/qrCode.js): `src/utils/qrCode.js`
- [Phone sensor client](https://github.com/hhaider3/motionlab/blob/main/src/components/MotionLab.jsx): `src/components/MotionLab.jsx`
- [Live telemetry](https://github.com/hhaider3/motionlab/blob/main/src/components/MotionLab.jsx): `src/components/MotionLab.jsx`
- [Sword game scene](https://github.com/hhaider3/motionlab/blob/main/src/components/MotionLab.jsx): `src/components/MotionLab.jsx`
- [Hit scoring](https://github.com/hhaider3/motionlab/blob/main/src/components/MotionLab.jsx): `src/components/MotionLab.jsx`
- [Phone vibration](https://github.com/hhaider3/motionlab/blob/main/src/components/MotionLab.jsx): `src/components/MotionLab.jsx`

### Motion relay

- [Relay endpoints](https://github.com/hhaider3/motionlab/blob/main/server/motion-relay-server.mjs): `server/motion-relay-server.mjs`
- [Session state](https://github.com/hhaider3/motionlab/blob/main/server/motion-relay-server.mjs): `server/motion-relay-server.mjs`
- [Sensor dispatch](https://github.com/hhaider3/motionlab/blob/main/server/motion-relay-server.mjs): `server/motion-relay-server.mjs`
- [Feedback dispatch](https://github.com/hhaider3/motionlab/blob/main/server/motion-relay-server.mjs): `server/motion-relay-server.mjs`

### Relay protocol

- [WebSocket framing](https://github.com/hhaider3/motionlab/blob/main/server/websocket-relay.mjs): `server/websocket-relay.mjs`

### Other components

- **Phone user**
- **Desktop player**

## Connections

- Desktop player → Route selector: opens app (evidence: `README.md`)
- Route selector → Desktop pairing: routes desktop (evidence: `src/App.jsx`)
- Route selector → Phone sensor client: routes phone (evidence: `src/App.jsx`)
- Desktop pairing → QR generation: generates code (evidence: `src/components/MotionLab.jsx`)
- Phone user → Phone sensor client: starts sensors (evidence: `README.md`)
- Desktop pairing → Phone sensor client: pairs session (evidence: `README.md`)
- Phone sensor client → Relay endpoints: publishes packets (evidence: `README.md`)
- Phone sensor client → WebSocket framing: streams packets (evidence: `README.md`)
- Relay endpoints → Session state: updates sessions (evidence: `server/motion-relay-server.mjs`)
- Relay endpoints → Sensor dispatch: routes sensor requests (evidence: `server/motion-relay-server.mjs`)
- Sensor dispatch → Session state: stores latest packet (evidence: `server/motion-relay-server.mjs`)
- Sensor dispatch → Live telemetry: streams sensor events (evidence: `README.md`)
- Live telemetry → Sword game scene: drives sword (evidence: `README.md`)
- Sword game scene → Hit scoring: detects cuts (evidence: `README.md`)
- Hit scoring → Relay endpoints: posts hit feedback (evidence: `README.md`)
- Relay endpoints → Feedback dispatch: routes feedback (evidence: `server/motion-relay-server.mjs`)
- Feedback dispatch → WebSocket framing: sends feedback (evidence: `server/motion-relay-server.mjs`)
- WebSocket framing → Phone sensor client: delivers feedback (evidence: `README.md`)
- Phone sensor client → Phone vibration: vibrates on hit (evidence: `README.md`)
- Phone user → Phone vibration: feels cut feedback (evidence: `README.md`)
- Relay endpoints → WebSocket framing: uses protocol helpers (evidence: `server/motion-relay-server.mjs`)
- Desktop player → Desktop pairing: scans QR (evidence: `README.md`)
