A real-time traffic control center simulation built as a 3rd year project. Models intersections using finite state machines and queue theory, with a live dashboard, adaptive AI mode, and role-based access control with user safety first.
- Real-time simulation — traffic signals cycle through states (FSM) with vehicle queue modeling per intersection
- Two control modes — Manual (operator-set timings) and Adaptive AI (auto-adjusts based on queue lengths)
- Live dashboard — WebSocket-powered updates for signal states, queue lengths, and throughput
- Analytics & History — charts for vehicle throughput, wait times, and historical simulation runs
- JWT Auth + RBAC — Admin operators can control the simulation; Viewers get read-only access
| Layer | Tech |
|---|---|
| Frontend | Next.js 16, React 19, TypeScript, Chart.js, Socket.IO client |
| Backend | Node.js, Express 5, TypeScript, Socket.IO |
| Database | PostgreSQL |
| Auth | JWT (jsonwebtoken) + bcryptjs |
| Dev DB | Docker + pgAdmin |
project_trafficSim/
├── docker-compose.yml # full stack (postgres + backend + frontend + pgadmin)
├── Dockerfile.backend / frontend
├── .github/workflows/ci.yml # lint + typecheck + test + docker smoke
├── backend/
│ ├── src/
│ │ ├── config/ # env.ts (zod), db.ts, logger.ts (pino), initDb.ts
│ │ ├── lib/ # errors.ts (AppError, asyncHandler)
│ │ ├── middleware/ # auth, validate (zod), rateLimit, requestId, errorHandler
│ │ ├── engine/ # simulationEngine, signalFSM, queueModel (pure domain)
│ │ ├── routes/ # auth, signals, simulation, history, analytics
│ │ ├── models/ # pg queries
│ │ └── websocket/ # Socket.IO + JWT auth
│ ├── database/ # schema.sql, seed.sql, migrate.ts
│ └── Dockerfile
├── frontend/
│ ├── src/
│ │ ├── app/ # Next.js App Router (dashboard, config, login, history, analytics)
│ │ ├── components/ # ui/ErrorBoundary, dashboard, layout
│ │ ├── lib/ # api.ts, socket.ts, env.ts
│ │ └── context/ # AuthContext
│ └── Dockerfile (standalone)
└── docs/ # archived plans + legacy demo
See INDUSTRY_STANDARD_PLAN.md for the full transformation audit and phased plan.
| Requirement | Version | Windows | macOS | Linux |
|---|---|---|---|---|
| Node.js | 20+ | winget install OpenJS.NodeJS or nvm-windows |
brew install node@20 or nvm |
nvm / package manager |
| npm | 10+ | bundled with Node | bundled | bundled |
| Docker | 24+ | Docker Desktop | Docker Desktop / Colima | Docker Engine |
.nvmrcand.node-versionboth point to20— usenvm use(macOS/Linux) ornvm use 20(Windows nvm-windows) orfnm use.
# 1. Configure env (all platforms: copy example, no shell-specific syntax)
# Windows (PowerShell/CMD): copy .env.example .env
# copy backend\.env.example backend\.env
# macOS/Linux: cp .env.example .env && cp backend/.env.example backend/.env
cp .env.example .env # set JWT_SECRET
cp backend/.env.example backend/.env # set ADMIN_PASSWORD before first start
cp frontend/.env.example frontend/.env.local
# 2. Start full stack (needs Docker Desktop / Engine running)
docker compose up --build
# Frontend: http://localhost:3000
# Backend: http://localhost:3001/api/health
# http://localhost:3001/api/ready
# pgAdmin (tools profile): docker compose --profile tools up -d
# Stop:
docker compose downWorks the same on Windows (PowerShell/CMD), macOS, Linux — no PGPASSWORD or Unix-only && required.
# 1. Database only (requires Docker)
npm run db:up # cross-platform: docker compose -f postgres-setup/docker-compose.yml up -d
# Alternative full-stack wait: npm run db:up:wait
# Configure backend/.env from backend/.env.example and set JWT_SECRET and
# ADMIN_PASSWORD before the first migration.
# 2. Backend — cross-platform DB setup (no `psql` CLI needed)
npm run db:setup --workspace=backend # runs node scripts/db-setup.mjs via pg (Windows/macOS/Linux)
# or: npm run db:migrate --workspace=backend
# 3. Run backend + frontend together (cross-platform)
npm run dev # → node scripts/dev.mjs (Windows-safe, no PowerShell && issue)
# Alternative: runs separately
npm run start:backend # → npm run dev --workspace=backend (tsx watch)
npm run start:frontend # → npm run dev --workspace=frontend (next dev)
# Or run individually:
# Terminal 1:
npm run dev --workspace=backend # http://localhost:3001
# Terminal 2:
npm run dev --workspace=frontend # http://localhost:3000Windows-specific notes
- Use PowerShell or CMD —
npm run devnow usesnode scripts/dev.mjsso&&andPGPASSWORD=...are not required. - If
dockernot found, install Docker Desktop and enable WSL2 backend. psqlis not required —scripts/db-setup.mjsuses Nodepgdriver.- Line endings are normalized to
LFvia.gitattributes:1— no CRLF issues on clone. - If
npm cifails on native deps, runnpm installornpm rebuild.
macOS / Linux notes
brew install node@20+docker/colimaworks.npm run dev:legacystill available for Unix shells if you prefer&&+concurrently.
npm run lint # eslint across workspaces
npm run typecheck # tsc --noEmit
npm run build # build both apps
npm run test # jest --forceExit (backend, 34 tests)
npm run format # prettier write
npm run db:up # docker compose postgres up -d
npm run db:down # docker compose postgres down
npm run dev # cross-platform dev (node scripts/dev.mjs)
npm run dev:docker # docker compose up --build (full stack)
npm run dev:docker:down # docker compose down| Var | Required | Default | Description |
|---|---|---|---|
JWT_SECRET |
✅ | — | Set a random secret of at least 32 characters |
ADMIN_PASSWORD |
✅ (first migration) | — | Unique initial admin password; set before first migration |
CORS_ORIGIN |
— | http://localhost:3000 |
Comma-separated allowlist |
DB_HOST/PORT/USER/PASSWORD/NAME |
— | localhost:5432/admin/mydb |
Postgres |
NEXT_PUBLIC_API_URL |
— | http://localhost:3001 |
Frontend API base |
See backend/.env.example and frontend/.env.example.
| Username | Password | |
|---|---|---|
admin |
[email protected] |
Set ADMIN_PASSWORD in the backend environment before starting the migrations |
The migrations hash this value and never store it in the seed SQL. For an existing
installation, set ADMIN_PASSWORD before the next startup so migration V007 can
replace the seeded admin password once.
| Endpoint | Method | Auth | Admin Only |
|---|---|---|---|
/api/docs |
GET | ❌ | ❌ |
/api/openapi.json |
GET | ❌ | ❌ |
/api/auth/login |
POST | ❌ | ❌ |
/api/auth/me |
GET | ✅ | ❌ |
/api/auth/register |
POST | ✅ | ✅ |
/api/simulation/status |
GET | ✅ | ❌ |
/api/simulation/start |
POST | ✅ | ✅ |
/api/simulation/stop |
POST | ✅ | ✅ |
/api/simulation/mode |
POST | ✅ | ✅ |
/api/signals |
GET | ✅ | ❌ |
/api/signals |
POST | ✅ | ✅ |
/api/analytics/summary |
GET | ✅ | ❌ |
/api/history |
GET | ✅ | ❌ |
WebSocket tick-update |
— | ✅ | ❌ |
GET /api/docs— Swagger UI (interactive API documentation)GET /api/openapi.json— OpenAPI 3.0 spec (raw JSON)
cd backend
npm testTests cover the simulation FSM, queue model, and API route protection.
MIT