How yuragb/server_for_cam_det works
Purpose and Entry Points
cam_serv is a Bun/Elysia WebRTC signaling service. It authenticates WebSocket upgrades, tracks connected peers, and relays SDP/ICE-style signaling payloads between peers; it does not carry media. Process startup is centered on src/server.ts, with application composition in src/Elysia/app.ts and Elysia bootstrap/export in src/Elysia/index.ts.
HTTP and WebSocket Surface
The route layer exposes root/health endpoints and the /ws signaling endpoint through src/Elysia/modules/Routes/index.ts and src/Elysia/modules/Routes/Signals/index.ts. WebSocket configuration, lifecycle handling, peer registration, forwarding, heartbeat cleanup, and connection limits are concentrated in:
• src/Elysia/modules/websockets/index.ts
• src/Elysia/modules/websockets/websocketConfig.ts
• src/Elysia/modules/websockets/wsLimits.ts
Control flow is: upgrade request → authenticate → connect socket → require register with peerId → map peer ID to socket → forward validated messages to targetPeerId with sender identity → remove peers on close/heartbeat expiry. Duplicate peer IDs replace the earlier connection.
Authentication and Request Security
JWT validation is applied at the Elysia module boundary, supporting bearer tokens and browser query-token fallback. Authentication components are:
• src/Elysia/modules/Authentication/index.ts
• src/Elysia/modules/Authentication/utils/index.ts
• src/Elysia/modules/Security/Cooclie/index.ts
Tokens may be validated via frontend-auth JWKS or shared-secret service JWTs, using issuer/audience and service-issuer environment configuration. Cross-cutting HTTP/security controls include rate limiting and security headers:
• src/Elysia/modules/Security/RateLimiter/index.ts
• src/Elysia/modules/Security/SecurityHeaders/index.ts
State, Persistence, and Cache Boundaries
Live signaling-peer state is inherently process-local WebSocket state. Redis is provisioned as infrastructure and is represented in application/cache modules:
• src/Redis/index.ts
• src/Elysia/modules/infrastructure/cache/cache.service.ts
• k3s/05-signaling-redis-deployment.yaml
SQLite/Drizzle persistence also exists, including a shallow-user schema and versioned migrations:
• src/db/drizzle/index.ts
• src/db/schema/ShalowUsers.ts
• drizzle/0001_lush_morlocks.sql
This separates transient connection routing from durable user-oriented data.
Runtime and Deployment
The service runs on Bun with Elysia, containerized by Dockerfile. It can run locally with Docker Compose or on k3s. Kubernetes manifests deploy separate signaling-app and Redis workloads, persistent volumes, services, secrets/config maps, and ingress:
• docker-compose.yml
• k3s/05-signaling-app-deployment.yaml
• k3s/09-ingress.yaml
Environment-driven limits include port, log level, maximum signaling-message bytes, JWT secret, issuer, audience, and allowed service issuers. /health supports probes and reports connected-peer state.
Supporting Service Boundaries
User synchronization is represented separately from realtime signaling:
• src/Elysia/modules/Routes/User/index.ts
• src/Elysia/modules/Routes/User/Controllers/sync.controller.ts
• src/Elysia/modules/Routes/User/Services/index.ts
src/cluster.ts indicates process/cluster orchestration as a runtime concern. Any scaling design must preserve peer-routing correctness: a sender and target located on different app instances need shared routing/session coordination, for which Redis is the likely infrastructure boundary.
Main entry points: app.ts Application composition · websockets/index.ts Peer signaling lifecycle · Routes/index.ts HTTP routes · Signals/index.ts Signaling endpoint
Components 27
Bun / Elysia service
- Process startup
src/server.ts - Elysia bootstrap
src/Elysia/index.ts - Application composition
src/Elysia/app.ts - HTTP routes · Provides root and health endpoints.
src/Elysia/modules/Routes/index.ts - Signaling endpoint · Exposes authenticated /ws upgrades.
src/Elysia/modules/Routes/Signals/index.ts - Peer signaling lifecycle · Registers peers, relays messages, and removes stale connections.
src/Elysia/modules/websockets/index.ts - Socket configuration
src/Elysia/modules/websockets/websocketConfig.ts - Connection limits
src/Elysia/modules/websockets/wsLimits.ts - JWT authentication · Accepts bearer or query-token credentials and validates JWT claims.
src/Elysia/modules/Authentication/index.ts - JWT validators · Uses JWKS or service shared-secret validation.
src/Elysia/modules/Authentication/utils/index.ts - Cookie security
src/Elysia/modules/Security/Cooclie/index.ts - Rate limiting
src/Elysia/modules/Security/RateLimiter/index.ts - Security headers
src/Elysia/modules/Security/SecurityHeaders/index.ts - User synchronization · Separate from realtime signaling.
src/Elysia/modules/Routes/User/index.ts - Sync controller
src/Elysia/modules/Routes/User/Controllers/sync.controller.ts - User services
src/Elysia/modules/Routes/User/Services/index.ts - Cluster orchestration
src/cluster.ts
State and persistence
- Redis cache boundary · Likely shared coordination boundary for multi-instance peer routing.
src/Redis/index.ts - Cache service
src/Elysia/modules/infrastructure/cache/cache.service.ts - Drizzle persistence
src/db/drizzle/index.ts - Shallow users schema
src/db/schema/ShalowUsers.ts - Initial migration
drizzle/0001_lush_morlocks.sql
Container and k3s
- Container image
Dockerfile - Local stack
docker-compose.yml - Signaling app workload
k3s/05-signaling-app-deployment.yaml - Redis workload
k3s/05-signaling-redis-deployment.yaml - Ingress · Publishes HTTP, health, and WebSocket traffic.
k3s/09-ingress.yaml
Connections 28
- Process startup → Elysia bootstrap: starts
- Elysia bootstrap → Application composition: exports
- Application composition → HTTP routes: mounts
- Application composition → JWT authentication: applies
- Application composition → Rate limiting: applies
- Application composition → Security headers: applies
- Application composition → Cookie security: applies
- HTTP routes → Signaling endpoint: routes /ws
- HTTP routes → User synchronization: routes user sync
- JWT authentication → JWT validators: validates with
- Signaling endpoint → JWT authentication: authenticates upgrade
- Signaling endpoint → Peer signaling lifecycle: opens socket
- Peer signaling lifecycle → Socket configuration: uses
- Peer signaling lifecycle → Connection limits: enforces
- Peer signaling lifecycle → Redis cache boundary: coordinates when scaled
- User synchronization → Sync controller: dispatches
- Sync controller → User services: uses
- User services → Drizzle persistence: persists users
- Drizzle persistence → Shallow users schema: maps
- Initial migration → Shallow users schema: creates
- Cache service → Redis cache boundary: connects
- Cluster orchestration → Process startup: orchestrates workers
- Container image → Process startup: packages
- Local stack → Container image: runs
- Signaling app workload → Container image: runs
- Signaling app workload → Redis workload: uses
- Redis workload → Redis cache boundary: provides Redis
- Ingress → Signaling app workload: routes traffic