How sanjitdev/surakkha works
Purpose and topology
Surakkha is a monorepo for real-time water-quality monitoring: simulated devices send telemetry, the backend persists/evaluates it, and a React dashboard presents live device, reading, incident, and simulator-control views. Workspace boundaries are packages/api, packages/web, packages/simulator, packages/db, and packages/shared; Compose runs API, web, simulator, and Postgres (docker-compose.yml, pnpm-workspace.yaml).
Runtime entry points
The API is a Node/TypeScript process rooted at packages/api/src/index.ts; it hosts HTTP routes and WebSocket ingestion/realtime behavior in one process. The device simulator starts from packages/simulator/src/index.ts and exposes its control surface through packages/simulator/src/control/server.ts. The browser SPA enters at packages/web/src/main.tsx, while Nginx serves its built assets (packages/web/nginx.conf).
Shared contracts boundary
packages/shared is the required cross-package contract layer; feature packages should not import each other’s internal types. Telemetry framing is defined in packages/shared/src/telemetry.ts, with related event, auth/RBAC, incident, rule, dashboard, and simulator models colocated under packages/shared/src/. This is the principal boundary between simulator, API, and web, and the telemetry contract is versioned/frozen at v1.
Telemetry ingest and fan-out
Devices/simulator connect over plain WebSocket to the API ingestion server (packages/api/src/ingest/server.ts) and submit frames validated/processed by packages/api/src/ingest/frame.ts. Ingestion includes sequencing and rate-limit controls (packages/api/src/ingest/sequence.ts, packages/api/src/ingest/rateLimit.ts), then publishes accepted readings through subscriber infrastructure (packages/api/src/ingest/subscriber.ts). The web client consumes live updates via packages/web/src/realtime/socketClient.ts and dashboard socket integration (packages/web/src/dashboard/useDashboardSocket.ts).
Persistence and rule evaluation
Postgres 15 is the sole durable store, accessed through Prisma schema/migrations in packages/db/prisma/schema.prisma and packages/db/prisma/migrations/. Database seed logic establishes initial devices, users, and thresholds/rules (packages/db/prisma/seed.ts, packages/db/prisma/thresholdTable.ts). The API rule stage loads/caches rule data and evaluates incoming readings (packages/api/src/rules/cache.ts, packages/api/src/rules/engine.ts, packages/api/src/rules/hooks.ts), forming the alert/incident trigger boundary.
API security and feature surfaces
Authentication uses HS256 JWT helpers and auth routes (packages/api/src/auth/jwt.ts, packages/api/src/auth/router.ts); authorization is centralized in packages/api/src/middleware/authorize.ts. API surfaces currently organize around devices, latest readings, recent incidents, and admin simulator control (packages/api/src/devices/router.ts, packages/api/src/readings/latestRouter.ts, packages/api/src/admin/simulatorRouter.ts). Audit behavior is centralized in packages/api/src/audit.ts, rather than embedded exclusively in individual routes.
User-facing flows
The dashboard composes device/map, live-reading, and recent-incident regions (packages/web/src/dashboard/Dashboard.tsx, packages/web/src/dashboard/MapRegion.tsx, packages/web/src/dashboard/LiveReadingsRegion.tsx). Authentication and role-aware navigation are implemented through the auth/access and shell layers (packages/web/src/auth/LoginShell.tsx, packages/web/src/access/RbacRoute.tsx, packages/web/src/shell/AppShell.tsx). Admins control simulator devices from packages/web/src/admin/simulator/SimulatorPage.tsx; that client calls API control endpoints, which communicate with the separate simulator process rather than bypassing the ingestion wire contract.
Infrastructure constraints
Development and local deployment use Docker Compose, with Postgres health-gated before the Node API and web served through Nginx (docker-compose.yml, packages/api/Dockerfile, packages/web/Dockerfile, packages/simulator/Dockerfile). The intended v1 architecture deliberately has no queue, Redis, or time-series database: ingestion, rules, workflows, and scheduled work remain in the API process, with Postgres as the system of record.
Main entry points: router.ts Feature and admin routes · index.ts API host · main.tsx React dashboard · server.ts Telemetry ingest
Components 19
Shared contracts
Required cross-package boundary.
- Shared v1 models · Telemetry, events, auth, incidents, rules, dashboard, and simulator models.
packages/shared/src/telemetry.ts
API process
- API host
packages/api/src/index.ts - Auth and authorization · JWT routes and centralized RBAC authorization.
packages/api/src/auth/router.ts - Feature and admin routes · Devices, latest readings, incidents, and simulator control.
packages/api/src/devices/router.ts - Telemetry ingest · Validates frames and applies sequencing and rate limits.
packages/api/src/ingest/server.ts - Realtime subscribers
packages/api/src/ingest/subscriber.ts - Rule evaluation · Caches rules and evaluates incoming readings.
packages/api/src/rules/engine.ts - Audit service
packages/api/src/audit.ts
Clients
- Device simulator
packages/simulator/src/index.ts - Simulator control server
packages/simulator/src/control/server.ts - React dashboard
packages/web/src/main.tsx - Dashboard regions · Device/map, live-reading, and incident views.
packages/web/src/dashboard/Dashboard.tsx - Auth and RBAC shell
packages/web/src/auth/LoginShell.tsx - Live socket client
packages/web/src/realtime/socketClient.ts - Simulator admin UI
packages/web/src/admin/simulator/SimulatorPage.tsx
Data and infrastructure
- Postgres 15 · Sole durable store for telemetry, devices, users, rules, and incidents.
packages/db/prisma/schema.prisma - Database seed data · Initial devices, users, thresholds, and rules.
packages/db/prisma/seed.ts - Docker Compose · Runs API, web, simulator, and health-gated Postgres.
docker-compose.yml - Nginx web server
packages/web/nginx.conf
Connections 28
- Device simulator → Simulator control server: hosts
- Device simulator → Shared v1 models: uses contracts
- Device simulator → Telemetry ingest: telemetry WebSocket
- React dashboard → Dashboard regions: renders
- React dashboard → Auth and RBAC shell: enforces access
- React dashboard → Simulator admin UI: admin view
- Dashboard regions → Live socket client: live updates
- Live socket client → Realtime subscribers: WebSocket
- Simulator admin UI → Feature and admin routes: control requests
- React dashboard → Shared v1 models: uses models
- API host → Auth and authorization: hosts
- API host → Feature and admin routes: hosts
- API host → Telemetry ingest: hosts
- Feature and admin routes → Auth and authorization: authorizes
- Feature and admin routes → Simulator control server: controls
- Feature and admin routes → Postgres 15: queries
- Feature and admin routes → Audit service: audits
- Telemetry ingest → Postgres 15: persists readings
- Telemetry ingest → Rule evaluation: accepted readings
- Telemetry ingest → Realtime subscribers: publishes
- Rule evaluation → Postgres 15: loads rules and stores incidents
- API host → Shared v1 models: uses contracts
- Database seed data → Postgres 15: initializes
- Nginx web server → React dashboard: serves build
- Docker Compose → Postgres 15: runs
- Docker Compose → API host: runs
- Docker Compose → Device simulator: runs
- Docker Compose → Nginx web server: runs