Skip to content

Latest commit

 

History

History
366 lines (272 loc) · 11.5 KB

File metadata and controls

366 lines (272 loc) · 11.5 KB

📹 WebRTC

Production-Ready WebRTC Learning Platform

Go CI Pages License: MIT Go Version WebRTC Docker

English | 简体中文 | 📖 Online Docs

From basic peer-to-peer calls to advanced multi-party Mesh architecture.
A learning-oriented, security-first WebRTC implementation in Go.

📋 Product Spec · 🏗️ RFCs · 📡 API Spec · 📝 Changelog · 🗺️ Roadmap


Table of Contents


✨ Features

Core Capabilities

  • 🌐 WebSocket Signaling — Gorilla WebSocket with heartbeat & room management
  • 👥 Multi-party Mesh — Up to 50 participants per room
  • 💬 DataChannel Chat — P2P messaging without server relay
  • 🎥 Media Controls — Mute/unmute, camera toggle, screen sharing
  • 📹 Local Recording — Browser-side MediaRecorder with WebM export

Production Ready

  • 🔒 Security-First — Origin validation, identity binding, rate limiting
  • 📦 Zero-Dependency Frontend — Pure vanilla JavaScript
  • 🐳 Docker Ready — Multi-stage builds for minimal image size
  • 🧪 Well Tested — Unit tests, e2e tests with Playwright
  • 📝 Bilingual Docs — Complete EN/ZH documentation

🚀 Quick Start

Prerequisites

  • Go 1.22+
  • Modern browser (Chrome 90+, Firefox 88+, Safari 14+)
  • Docker (optional)

Option 1: Run Locally (Fastest)

# Clone and run
git clone https://github.com/LessUp/webrtc.git
cd webrtc
go mod tidy
go run ./cmd/server

# Open http://localhost:8080

Option 2: Docker

docker build -t webrtc .
docker run --rm -p 8080:8080 webrtc

Option 3: Docker Compose (Production)

export DOMAIN=your-domain.com
docker compose up -d

Visit https://your-domain.com with automatic HTTPS via Caddy.

Usage

  1. Open two browser windows to http://localhost:8080
  2. Enter the same room name and click Join
  3. Select a peer from the member list and click Call
  4. Allow camera/microphone permissions ✅
  5. Enjoy video calls, screen sharing, and text chat!
💡 Need help with NAT traversal?

For connections across different networks, configure a TURN server:

export RTC_CONFIG_JSON='{
  "iceServers": [
    { "urls": ["stun:stun.l.google.com:19302"] },
    { "urls": ["turn:your-turn.com:3478"], "username": "user", "credential": "pass" }
  ]
}'

See Deployment Guide for TURN setup.


🏗️ Architecture

This project follows Spec-Driven Development (SDD) — all implementation is driven by specifications.

System Overview

┌────────────────────────────────────────────────────────────┐
│  Browser A                                                  │
│  ┌────────┐    ┌──────────┐    ┌────────────────────┐     │
│  │HTML UI │──→ │  app.js  │──→ │   getUserMedia     │     │
│  └────────┘    └────┬─────┘    └─────────┬──────────┘     │
└─────────────────────┼────────────────────┼────────────────┘
                      │ WebSocket          │ WebRTC P2P
               ┌──────▼──────┐             │
               │  Go Server   │             │
               │ ┌─────────┐ │             │
               │ │Hub/Room │ │             │
               │ │Manager  │ │             │
               │ └─────────┘ │             │
               └──────┬──────┘             │
                      │ WebSocket          │
┌─────────────────────┼────────────────────┼────────────────┐
│  Browser B          │                    │                │
│  ┌────────┐    ┌────▼──────┐    ┌───────▼────────────┐   │
│  │HTML UI │──→ │  app.js   │──→ │   getUserMedia     │   │
│  └────────┘    └───────────┘    └────────────────────┘   │
└────────────────────────────────────────────────────────────┘
Flow Technology Description
Signaling WebSocket /ws Offer/Answer/ICE relay via Go Hub
Media WebRTC P2P Audio/video streams (direct browser-to-browser)
DataChannel WebRTC P2P Text chat (direct browser-to-browser)

Project Structure

webrtc/
├── cmd/server/              # HTTP + WebSocket entry point
├── internal/signal/         # Signaling logic
│   ├── hub.go               # Room management, message relay
│   ├── hub_test.go          # Unit tests
│   └── message.go           # Message types
├── web/                     # Frontend (vanilla JS)
│   ├── src/
│   │   ├── core/            # App initialization
│   │   └── controllers/     # UI, media, signaling, peers
│   ├── index.html
│   └── styles.css
├── docs/                    # Documentation (EN/ZH)
├── specs/                   # Specifications (SDD)
│   ├── product/             # Feature specs
│   ├── rfc/                 # Architecture RFCs
│   ├── api/                 # OpenAPI specs
│   ├── db/                  # Schema definitions
│   └── testing/             # BDD test specs
└── deploy/                  # Deployment configs

Specifications

Document Purpose
Product Spec Feature definitions, user stories, acceptance criteria
RFC-0001 Signaling server architecture decisions
RFC-0002 Frontend module design and state management
API Spec OpenAPI 3.0 signaling protocol specification
DB Schema In-memory data structure definitions
Testing Spec BDD acceptance criteria

📚 Documentation

📖 Guides

Document Description
Guide Architecture deep-dive, implementation walkthrough
Signaling Protocol WebSocket message formats and flow
Deployment Docker, HTTPS, TURN server setup
API Reference Environment variables and configuration
Troubleshooting Common issues and solutions

🌐 Online Documentation

https://lessup.github.io/webrtc/

Complete documentation with search, navigation, and bilingual support.


⚙️ Configuration

Environment Variables

Variable Default Description
ADDR :8080 HTTP listen address
WS_ALLOWED_ORIGINS * Allowed origins (comma-separated, * for all)
RTC_CONFIG_JSON Public STUN ICE/TURN config (JSON) passed to browser

ICE/TURN Configuration Example

export RTC_CONFIG_JSON='{
  "iceServers": [
    { "urls": ["stun:stun.l.google.com:19302"] },
    { 
      "urls": ["turn:turn.yourdomain.com:3478"],
      "username": "your_username",
      "credential": "your_password"
    }
  ]
}'

🚀 Deployment

Production Checklist

  • Set WS_ALLOWED_ORIGINS to your domain (security)
  • Configure TURN server for NAT traversal
  • Enable HTTPS (Caddy auto-handles this)
  • Set up monitoring and log rotation

Docker Compose (Recommended)

# docker-compose.yml
services:
  webrtc:
    build: .
    environment:
      - WS_ALLOWED_ORIGINS=yourdomain.com
      - RTC_CONFIG_JSON={"iceServers":[...]}
  
  caddy:
    image: caddy:2-alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./deploy/web/Caddyfile:/etc/caddy/Caddyfile
  
  coturn:
    image: coturn/coturn:latest
    network_mode: host

See Deployment Guide for detailed instructions.


🤝 Contributing

We welcome contributions! This project follows Spec-Driven Development (SDD):

  1. 📝 Read the relevant specs in /specs first
  2. 💡 Propose spec changes if needed
  3. 💻 Implement according to specs
  4. ✅ Test against acceptance criteria

Quick Links

Development

# Setup
go mod tidy

# Run tests
go test -race ./...

# Run linter
golangci-lint run

# Start with hot reload
air

🛡️ Security

  • ✅ Origin whitelist validation
  • ✅ Server-verified client identities
  • ✅ Connection limits (50/room, 1000 rooms max)
  • ✅ Input validation and sanitization

See Security Policy for reporting vulnerabilities.


📊 Project Stats

Metric Value
Language Go / JavaScript
Lines of Code ~3,000 Go + ~2,000 JS
Test Coverage Core modules tested
License MIT

📄 License

MIT License © LessUp


⭐ Star this repo if you find it helpful!

Made with ❤️ by LessUp

GitHub Stars