Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

AxioVerse - 2D Metaverse Platform

AxioVerse is a high-performance, real-time 2D metaverse platform that enables users to explore virtual spaces, interact with physics-based worlds, and connect via proximity-based video and audio chat. It is deployed as a cloud-native microservice system on Microsoft Azure Container Apps.


🏗️ High-Level Architecture

                         Users
                           │
                           ▼
                Azure Container Apps
                           │
      ┌──────────────┬───────────────┬──────────────┐
      │              │               │              │
      ▼              ▼               ▼              ▼
 Frontend        HTTP API        WS Server      Go Engine
 (React/Vite)  (Node/Express)   (Socket.IO)       (Go)
      │              │               │              │
      └──────────────┴───────┬───────┴──────────────┘
                             │
                             ▼
                    PostgreSQL Database
                          (Neon)
                             │
                             ▼
                    LiveKit Cloud (WebRTC)
                Voice & Video Communication

📂 Service Directory

Path Service Tech Stack Responsibility
apps/frontend Client React, Vite, Phaser, Nginx UI, game rendering, auth, API & socket clients
apps/http REST API Express, Prisma, JWT Auth, spaces, avatars, database CRUD
apps/ws Signal Node.js, Socket.IO Multiplayer sync, chat, presence, room lifecycle
apps/go-realtime-engine Game Engine Go Real-time state, movement, collision, physics, low-latency sync
packages/db Database Prisma Shared database access layer

🛠️ Prerequisites

  • Node.js (v18+)
  • Go (v1.20+)
  • PostgreSQL (Neon cloud or local)
  • LiveKit Server (installed via Homebrew or Binary)
  • Docker (for containerized deployment)

🚀 Getting Started (Local Dev)

1. Installation

npm install

2. Database Setup

npx prisma generate --schema packages/db/prisma/schema.prisma

3. Run All Services

npm run dev

Or run individual services:

  • npm run dev inside any apps/* folder
  • go run main.go inside apps/go-realtime-engine

📶 Multi-Device & LAN Testing

  1. Frontend: Run npm run dev and open the Network URL (e.g., http://172.20.10.2:5173).
  2. Backends: All services bind to 0.0.0.0 to accept external connections.
  3. LiveKit:
    livekit-server --dev --bind 0.0.0.0 --node-ip <YOUR_MAC_IP>

Tip

Dynamic IP Detection: The frontend auto-derives backend URLs from window.location.hostname for local/LAN dev. For production, use VITE_* environment variables injected at build time.


🔑 Environment Variables

Runtime Variables (.env)

# PostgreSQL credentials (docker-compose local dev)
POSTGRES_DB=mydb
POSTGRES_USER=postgres
POSTGRES_PASSWORD=mysecretpassword

DATABASE_URL="postgresql://postgres:mysecretpassword@localhost:5432/mydb"

JWT_PASSWORD="your-secret-password"
JWT_SECRET_ADMIN="your-admin-jwt-secret"
JWT_SECRET_USER="your-user-jwt-secret"

IMAGEKIT_PUBLIC_KEY="your-key"
IMAGEKIT_PRIVATE_KEY="your-key"

PORT=3000
WS_PORT=8081

Frontend Build-Time Variables (VITE_*)

Injected as Docker ARGs at build time — baked into the static bundle. Not available at runtime.

Variable Description
VITE_BACKEND_URL HTTPS URL to the HTTP API Container App
VITE_WS_URL WSS URL to the WebSocket Container App
VITE_GO_URL HTTPS URL to the Go Engine Container App
VITE_LIVEKIT_URL WSS URL to the LiveKit server

🔐 Authentication

HTTP Auth Flow

Signup/Login
     ↓
Express API
     ↓
JWT Generation
     ↓
Frontend stores JWT
     ↓
Every HTTP request → Authorization: Bearer <token>
     ↓
Protected APIs

WebSocket Auth

WebSocket connections are also JWT-protected. On join_room, the client sends its JWT token; the server calls jwt.verify() and closes the connection if invalid. All subsequent chat and movement messages are trusted based on the authenticated session.


🎮 Multiplayer Flow

Player moves
     ↓
Frontend
     ↓
Socket.IO (WS Server)
     ↓
Go Engine (state update)
     ↓
Broadcast updated positions
     ↓
All connected players

Only state changes are transmitted — latency remains low.


🎙️ Voice & Video

Uses LiveKit Cloud via WebRTC.

Frontend
     ↓
LiveKit SDK
     ↓
LiveKit Cloud
     ↓
WebRTC → Proximity Voice + Video Chat

Note

Media does not pass through your backend. Only JWT/token generation is handled by the Go Engine. All audio/video is routed directly through LiveKit Cloud.


🐳 Docker / Local Container Testing

All services have production-ready multi-stage Dockerfiles in docker/. Start the full stack locally:

docker compose up --build

Container Overview

Container Dockerfile Port Notes
httpContainer docker/Dockerfile.http 3000 Waits for Postgres healthcheck
wsContainer docker/Dockerfile.ws 8081 Waits for Postgres healthcheck
goEngineContainer docker/Dockerfile.go 8082 Standalone, no DB dependency
frontendContainer docker/Dockerfile.frontend 80 Nginx serves Vite static build
metaverseDBContainer postgres (official) 5432 Named volume for persistence

Local VITE_* Overrides (in docker-compose.yml)

args:
  - VITE_BACKEND_URL=http://localhost:3000
  - VITE_WS_URL=ws://localhost:8081
  - VITE_GO_URL=http://localhost:8082
  - VITE_LIVEKIT_URL=ws://localhost:7880

Important

Replace these with actual ACA domain names for Azure deployments.


☁️ Azure Deployment

Service URL
Frontend https://axio-frontend.politesky-92f62407.eastasia.azurecontainerapps.io
HTTP API https://axio-http.politesky-92f62407.eastasia.azurecontainerapps.io
WebSocket Server wss://axio-ws.politesky-92f62407.eastasia.azurecontainerapps.io
Go Engine https://axio-go.politesky-92f62407.eastasia.azurecontainerapps.io
LiveKit Cloud wss://metaverse-video-lkhpfqfl.livekit.cloud

Azure Services Used

Azure Container Registry (ACR)

Central Docker image repository. All images are built, tagged, and pushed to ACR before deployment.

Image Tag
frontend v2
http v1
ws v1
go-engine v1

Azure Container Apps (ACA)

Four independent microservices, each as a separate Container App with external HTTPS ingress, managed TLS, and independent scaling.


Deployment Architecture

                    Azure Container Apps Environment
   ┌─────────────────────────────────────────────────────┐
   │                                                     │
   │  ┌─────────────┐  ┌─────────────┐  ┌───────────┐  │
   │  │  Frontend   │  │  HTTP API   │  │  WS Server│  │
   │  │ nginx:alpine│  │node:22-alpine│  │node:alpine│  │
   │  │   Port 80   │  │  Port 3000  │  │ Port 8081 │  │
   │  └─────────────┘  └─────────────┘  └───────────┘  │
   │                                                     │
   │               ┌───────────────┐                     │
   │               │   Go Engine   │                     │
   │               │    alpine     │                     │
   │               │   Port 8082   │                     │
   │               └───────────────┘                     │
   └─────────────────────────────────────────────────────┘
              │                        │
              ▼                        ▼
     Neon PostgreSQL             LiveKit Cloud

Deployment Pipeline

Code
 ↓
Docker Buildx (linux/amd64)
 ↓
Azure Container Registry (ACR)
 ↓
Azure Container Apps
 ↓
Public HTTPS Endpoint

Each service is independently deployable — a change to the Go Engine does not require rebuilding the frontend.


Step 1: Build & Push to ACR

# Log in
az acr login --name <YOUR_ACR_NAME>

# Backend services
docker buildx build --platform linux/amd64 \
  -f docker/Dockerfile.http \
  -t <YOUR_ACR_NAME>.azurecr.io/http:v1 . --push

docker buildx build --platform linux/amd64 \
  -f docker/Dockerfile.ws \
  -t <YOUR_ACR_NAME>.azurecr.io/ws:v1 . --push

docker buildx build --platform linux/amd64 \
  -f docker/Dockerfile.go \
  -t <YOUR_ACR_NAME>.azurecr.io/go-engine:v1 . --push

# Frontend — VITE_* args must point to ACA service URLs
docker buildx build --platform linux/amd64 \
  -f docker/Dockerfile.frontend \
  --build-arg VITE_BACKEND_URL=https://<http-app>.azurecontainerapps.io \
  --build-arg VITE_WS_URL=wss://<ws-app>.azurecontainerapps.io \
  --build-arg VITE_GO_URL=https://<go-app>.azurecontainerapps.io \
  --build-arg VITE_LIVEKIT_URL=wss://<your-livekit-host> \
  -t <YOUR_ACR_NAME>.azurecr.io/frontend:v2 . --push

Step 2: Environment Variables per Container App

HTTP API

DATABASE_URL         = <Neon PostgreSQL SSL connection string>
JWT_PASSWORD         = <secret>
JWT_SECRET_ADMIN     = <secret>
JWT_SECRET_USER      = <secret>
IMAGEKIT_PUBLIC_KEY  = <key>
IMAGEKIT_PRIVATE_KEY = <key>
PORT                 = 3000

WebSocket Server

DATABASE_URL  = <Neon PostgreSQL SSL connection string>
JWT_PASSWORD  = <secret>
WS_PORT       = 8081

Go Engine

PORT               = 8082
LIVEKIT_API_KEY    = <key>
LIVEKIT_API_SECRET = <secret>

Frontend — no runtime env vars; all config is baked in at build time.


Step 3: Ingress & Networking

  • Enable external ingress on all four Container Apps.
  • For the WebSocket service, ensure the ACA ingress supports persistent WebSocket connections.
  • Configure CORS on the HTTP and WS services to allow the Frontend Container App domain.
  • Database connection is secured over SSL (Neon provides this by default).

Scaling

Azure Container Apps supports independent, automatic scaling per service. Each microservice can scale without affecting others:

Scenario Scale Target
High REST API traffic HTTP service
Many concurrent WebSocket users WS service
Heavy real-time game load Go Engine
Static asset traffic Frontend (Nginx handles most of this at the edge)

ACA provides: independent scaling, isolated deployments, rolling revisions, automatic HTTPS, managed ingress, and container orchestration.


🔧 Key Docker Fixes for Azure Compatibility

1. npm ci with Lockfile (Dockerfile.http)

turbo prune --docker only outputs pruned package.json files to out/json/ — without the lockfile, npm install does a fresh resolution that can drop transitive deps (e.g., finalhandler required by Express).

COPY --from=builder /app/out/json/ .
COPY --from=builder /app/package-lock.json ./package-lock.json
RUN npm ci   # deterministic, lockfile-driven install

2. Correct turbo prune Package Name (Dockerfile.frontend)

The package name in apps/frontend/package.json is react-client, not frontend. Using the wrong name caused: Package with name frontend not found.

RUN turbo prune react-client --docker
RUN npx turbo run build --filter=react-client...

3. TypeScript Check Skipped for Production Builds

Existing non-critical TypeScript errors blocked the Docker build. tsc is no longer run as part of the build step:

"build": "vite build"

4. Go Engine Dockerfile Created

No Dockerfile existed for apps/go-realtime-engine. docker/Dockerfile.go was created as a two-stage build: golang:alpine to compile, alpine:latest to run.

5. Frontend API Config Updated for Azure

config/api.ts previously derived backend URLs from window.location.hostname — incompatible with ACA where each service has an unrelated domain. It now prioritises VITE_* env vars:

export const HTTP_API_URL = import.meta.env.VITE_BACKEND_URL || getBaseUrl('3000');
export const WS_URL       = import.meta.env.VITE_WS_URL       || getBaseUrl('8081', 'ws');
export const GO_API_URL   = import.meta.env.VITE_GO_URL       || getBaseUrl('8082');
export const LIVEKIT_URL  = import.meta.env.VITE_LIVEKIT_URL  || getBaseUrl('7880', 'ws');

🎨 Branding

The platform uses custom hand-drawn sprites (Warrior, Mage, Rogue) and a vibrant "Axio" design system powered by Tailwind and Vanilla CSS.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages