A full-stack 2D metaverse application built with TypeScript, featuring real-time multiplayer interactions, customizable spaces, and user authentication. Built as a monorepo using Turborepo with Bun as the package manager.
- Real-time Multiplayer: WebSocket-based real-time communication for user interactions
- Space Management: Create and manage custom 2D spaces with dynamic elements
- User Authentication: JWT-based authentication with role-based access control (Admin/User)
- Avatar System: Customizable user avatars
- Map System: Pre-built maps with reusable elements
- Admin Dashboard: Administrative controls for space and element management
- Containerized Deployment: Full Docker support for easy deployment
This is a Turborepo monorepo with the following structure:
-
apps/http- REST API server (Express.js)- Handles authentication, user management, and space operations
- Runs on port
3001 - Built with Express.js and TypeScript
-
apps/ws- WebSocket server- Real-time communication for multiplayer interactions
- Runs on port
3003 - Built with Node.js WebSocket library
-
apps/web- Next.js frontend- User interface for the metaverse
- Runs on port
3000 - Built with Next.js 16 and React 19
-
packages/db- Database layer- Prisma ORM with PostgreSQL
- Shared database client and schema
- Migrations and database management scripts
-
packages/schemas- Validation schemas- Zod schemas for request/response validation
- Shared across HTTP and WS servers
-
packages/ui- UI component library- Shared React components
- Used by the web application
-
packages/eslint-config- Shared ESLint configurations -
packages/typescript-config- Shared TypeScript configurations -
packages/tests- Test suite
The application uses PostgreSQL with the following main entities:
- User: User accounts with authentication and role-based access
- Space: User-created 2D spaces with dimensions and thumbnails
- Element: Reusable visual elements that can be placed in spaces
- SpaceElements: Junction table linking elements to spaces with coordinates
- Map: Pre-built map templates
- Avatar: User avatar images and metadata
- Runtime: Bun 1.2.5
- Language: TypeScript 5.9.2
- Monorepo: Turborepo 2.6.3
- Backend: Express.js 5.2.1
- Frontend: Next.js 16.0.10, React 19.2.0
- Database: PostgreSQL 16 with Prisma ORM 7.2.0
- WebSocket: ws 8.19.0
- Authentication: JWT (jsonwebtoken 9.0.3)
- Validation: Zod 4.3.5
- Containerization: Docker & Docker Compose
- Bun >= 1.2.5
- Docker and Docker Compose (for containerized setup)
- Node.js >= 18 (if not using Docker)
- PostgreSQL 16 (if not using Docker)
The easiest way to run the entire application:
# Clone the repository
git clone <repository-url>
cd 2d-metaverse
# Create environment file
cp .env.example .env
# Start all services
docker-compose up -d
# View logs
docker-compose logs -f
# Stop all services
docker-compose downServices will be available at:
- Frontend: http://localhost:3000
- HTTP API: http://localhost:3001
- WebSocket: ws://localhost:3003
- PostgreSQL: localhost:5432
# Install dependencies
bun install
# Set up environment variables
cp .env.example .env
# Edit .env with your database credentials
# Generate Prisma client
cd packages/db
bunx prisma generate
bunx prisma migrate dev
# Start all services in development mode
cd ../..
bun dev
# Or start individual services
bun dev --filter=@repo/http # HTTP server only
bun dev --filter=@repo/ws # WebSocket server only
bun dev --filter=@repo/web # Frontend onlybun dev # Start all apps in development mode
bun build # Build all apps and packages
bun start # Start all production builds
bun lint # Lint all packages
bun test # Run all tests
bun format # Format code with Prettier
bun check-types # Type check all packagescd packages/db
bunx prisma generate # Generate Prisma client
bunx prisma migrate dev # Create and apply migrations
bunx prisma studio # Open Prisma Studio GUIdocker-compose up -d # Start all services
docker-compose down # Stop all services
docker-compose logs -f [service] # View logs
docker-compose restart [service] # Restart a service
docker-compose ps # View running servicesPOST /api/v1/signup # User registration
POST /api/v1/signin # User login
GET /api/v1/avatars # Get all avatars
GET /api/v1/user/metadata # Get user metadata
# Space Management
POST /api/v1/space # Create a new space
DELETE /api/v1/space/:spaceId # Delete a space
GET /api/v1/space/all # Get all spaces
GET /api/v1/space/:spaceId # Get space details
POST /api/v1/space/element # Add element to space
DELETE /api/v1/space/element # Remove element from space
# Admin Routes
POST /api/v1/admin/element # Create element (Admin)
PUT /api/v1/admin/element/:id # Update element (Admin)
POST /api/v1/admin/avatar # Create avatar (Admin)
POST /api/v1/admin/map # Create map (Admin)
Real-time events for:
- User movement in spaces
- Space updates
- User interactions
Required environment variables:
# Database
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/metaverse
# Authentication
JWT_SECRET_KEY=your-secret-key-change-in-production
# Server Ports
PORT=3001 # HTTP server port
# Frontend URLs
NEXT_PUBLIC_HTTP_URL=http://localhost:3001
NEXT_PUBLIC_WS_URL=ws://localhost:30032d-metaverse/
βββ apps/
β βββ http/ # Express REST API
β β βββ src/
β β β βββ index.ts # Server entry point
β β β βββ middleware/ # Authentication middleware
β β β βββ routes/v1/ # API routes
β β β βββ types/ # TypeScript types
β β β βββ utils/ # Helper functions
β β βββ Dockerfile
β β βββ package.json
β β
β βββ ws/ # WebSocket server
β β βββ src/
β β β βββ index.ts # WS server entry point
β β β βββ RoomManager.ts # Room management logic
β β β βββ User.ts # User connection handling
β β β βββ types/
β β βββ Dockerfile
β β βββ package.json
β β
β βββ web/ # Next.js frontend
β βββ app/ # App directory (Next.js 13+)
β βββ public/
β βββ Dockerfile
β βββ package.json
β
βββ packages/
β βββ db/ # Prisma database layer
β β βββ prisma/
β β β βββ schema.prisma # Database schema
β β β βββ migrations/ # Database migrations
β β βββ generated/ # Generated Prisma client
β β βββ index.ts
β β βββ package.json
β β
β βββ schemas/ # Zod validation schemas
β β βββ index.ts
β β βββ package.json
β β
β βββ ui/ # Shared React components
β β βββ src/
β β β βββ button.tsx
β β β βββ card.tsx
β β β βββ code.tsx
β β βββ package.json
β β
β βββ eslint-config/ # ESLint configurations
β βββ typescript-config/ # TypeScript configurations
β βββ tests/ # Test suite
β
βββ conf/
β βββ nginx.conf # Nginx reverse proxy configuration
β
βββ docker-compose.yml # Docker orchestration
βββ turbo.json # Turborepo configuration
βββ package.json # Root package configuration
bun test # Run all tests
bun test --filter=@repo/tests # Run specific test package