How alokranjan89/nightshield works
Purpose and Runtime Topology
NightShield is a browser-based personal-safety system: users or guests create SOS sessions with location, and connected nearby users receive real-time alerts. The React/Vite client runs separately from a Node/Express API with Socket.IO. MongoDB persists users, contacts, and SOS state; Cloudinary stores evidence.
• Client bootstrap: client/src/main.jsx, client/src/App.jsx
• Server bootstrap: server/server.js, server/src/app.js
• Deployment targets: client/vercel.json, render.yaml
Client Application and User Flows
React Router pages implement the primary workflows: public home/login, dashboard, contact management, active SOS handling, history/status, and settings. Shared UI includes the navigation, SOS control, alerts, and status display.
• Workflow pages: client/src/pages/Home.jsx, client/src/pages/Dashboard.jsx, client/src/pages/SOSActive.jsx
• Shared emergency UI: client/src/components/SOSButton.jsx, client/src/components/AlertPopup.jsx, client/src/components/StatusIndicator.jsx
SOS Orchestration Boundary
The client’s SOS domain is centralized around context and hooks. A press-and-hold interaction triggers creation; browser geolocation is collected where permitted; client state drives the active SOS screen and later resolution/evidence behavior. REST is used for durable commands and history, while sockets deliver immediate peer notifications.
• State/control layer: client/src/context/SOSContext.jsx, client/src/hooks/useSOS.js, client/src/hooks/useHold.js
• Location capture: client/src/hooks/useLocation.js
• Transport clients: client/src/services/api.js, client/src/services/socket.js
API, Authorization, and Request Pipeline
Express mounts domain routes for SOS, contacts, and users. Clerk middleware protects account-scoped operations and enforces that a user can only operate on their own saved contacts, location, SOS history, resolution, and evidence. SOS creation permits guest use while optionally associating authenticated identity. App-level middleware supplies Helmet, CORS, request limits, and rate limiting.
• Route boundary: server/src/routes/sosRoutes.js, server/src/routes/contactRoutes.js, server/src/routes/userRoutes.js
• Domain handlers: server/src/controllers/sosController.js, server/src/controllers/contactController.js, server/src/controllers/userController.js
• Security/upload middleware: server/src/middleware/authMiddleware.js, server/src/middleware/uploadMiddleware.js
Persistence and External Services
Mongoose models form the durable domain: user location supports nearby matching, contacts are account-owned, and SOS records retain lifecycle and evidence references. MongoDB is the system of record. Cloudinary receives multipart photo/video evidence after authenticated upload handling. Clerk supplies JWT identity on REST requests and socket handshakes.
• Data models: server/src/models/User.js, server/src/models/Contact.js, server/src/models/SOS.js
• Service configuration: server/src/config/db.js, server/src/config/cloudinary.js, server/src/config/env.js
Real-Time Alert Distribution
Socket.IO is attached to the server runtime. Authenticated socket connections are tracked in an in-memory socket store. On SOS creation, server logic identifies nearby connected users using saved location and broadcasts SOS_ALERT; it can fall back to connected recipients when no nearby stored location exists. Resolving an SOS emits SOS_RESOLVED to previously notified users.
• Socket/runtime ownership: server/server.js, server/src/socketStore.js
• Event-producing domain logic: server/src/controllers/sosController.js
• Client socket consumption: client/src/services/socket.js, client/src/socket.js
Key Operational Constraints
Guest SOS works without account persistence features; authenticated workflows depend on Clerk tokens. Geolocation depends on browser permission. Nearby alerts depend on both persisted location and active socket presence, so the socket store is process-local rather than durable. Environment configuration defines API/socket origins, MongoDB, Clerk, Cloudinary, rate limits, and matching radius.
• Origin/environment boundary: server/src/config/origin.js, server/.env.example, client/.env.example
Main entry points: authMiddleware.js Auth & upload middleware · sosController.js SOS lifecycle & matching · SOSContext.jsx SOS state orchestration · server.js Server runtime
Components 20
React Client
- App shell & routes · Hosts public, dashboard, SOS, history, and settings flows.
client/src/App.jsx - SOS state orchestration · Coordinates SOS lifecycle and active-screen state.
client/src/context/SOSContext.jsx - Press-and-hold trigger
client/src/hooks/useHold.js - Browser location capture · Uses browser permission-gated geolocation.
client/src/hooks/useLocation.js - Emergency controls & alerts · Provides SOS activation, incoming alerts, and status display.
client/src/components/SOSButton.jsx - REST client · Sends durable SOS, contact, user, history, resolution, and evidence commands.
client/src/services/api.js - Socket client · Consumes immediate SOS alert and resolution events.
client/src/services/socket.js
Node API
- Server runtime · Owns Express and the Socket.IO server.
server/server.js - Express request pipeline · Applies security, CORS, limits, and mounted domain routes.
server/src/app.js - Domain routes · Exposes SOS, contacts, and user endpoints.
server/src/routes/sosRoutes.js - Auth & upload middleware · Verifies Clerk identity for account-scoped operations and handles evidence uploads.
server/src/middleware/authMiddleware.js - SOS lifecycle & matching · Creates and resolves SOS records, finds recipients, and emits events.
server/src/controllers/sosController.js - Contacts & user handlers · Enforces account-owned contact and user operations.
server/src/controllers/contactController.js - Connected socket store · Tracks authenticated active connections; it is process-local.
server/src/socketStore.js - SOS records · Persists SOS lifecycle, location, notified recipients, and evidence references.
server/src/models/SOS.js - Users & saved location · Supplies persisted identity and location for nearby matching.
server/src/models/User.js - Account contacts
server/src/models/Contact.js
External Services
- MongoDB
- Clerk · Supplies JWT identity for REST and socket handshakes.
- Cloudinary · Stores uploaded photo and video evidence.
Connections 24
- App shell & routes → SOS state orchestration: drives SOS flow
- Emergency controls & alerts → Press-and-hold trigger: press and hold
- Press-and-hold trigger → SOS state orchestration: triggers creation
- Browser location capture → SOS state orchestration: provides location
- SOS state orchestration → REST client: durable commands
- SOS state orchestration → Socket client: real-time events
- REST client → Server runtime: HTTPS
- Socket client → Server runtime: Socket.IO
- Server runtime → Express request pipeline: hosts
- Express request pipeline → Domain routes: mounts
- Domain routes → Auth & upload middleware: protects uploads and accounts
- Auth & upload middleware → SOS lifecycle & matching: authorized SOS requests
- Auth & upload middleware → Contacts & user handlers: authorized account requests
- Auth & upload middleware → Clerk: verifies JWT
- SOS lifecycle & matching → SOS records: creates and resolves
- SOS lifecycle & matching → Users & saved location: nearby lookup
- Contacts & user handlers → Account contacts: owns contacts
- SOS records → MongoDB: persists
- Users & saved location → MongoDB: persists
- Account contacts → MongoDB: persists
- SOS lifecycle & matching → Connected socket store: finds connected recipients
- Server runtime → Connected socket store: tracks authenticated sockets
- SOS lifecycle & matching → Server runtime: emits SOS_ALERT and SOS_RESOLVED
- Auth & upload middleware → Cloudinary: uploads evidence