Document Version: 1.0
Last Updated: January 2026
Purpose: System audit and modernization planning
Nightscout (cgm-remote-monitor) is an open-source, real-time Continuous Glucose Monitoring (CGM) data visualization system. It enables patients and caregivers to remotely monitor blood glucose levels, receive alerts, and track diabetes management data.
- Version: 15.0.4
- License: AGPL-3.0
- Primary Stack: Node.js + MongoDB + Socket.IO
- Node.js Support: ^14.x, ^16.x, ^18.x, ^20.x (LTS versions)
- Supported NPM: ^6.x
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CLIENT LAYER β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Web Dashboard β Pebble Watch β Mobile Apps β Alexa/Google Home β
β (D3.js/jQuery) β (/pebble API) β (REST/Socket) β (Voice Assistants) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β TRANSPORT LAYER β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β HTTP/HTTPS (Express 4.17.1) β Socket.IO 4.5.4 β
β REST API v1/v2/v3 β /storage, /alarm namespaces β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β APPLICATION LAYER β
ββββββββββββββββββ¬βββββββββββββββββ¬βββββββββββββββββ¬ββββββββββββββββββββββββββ€
β Authorization β Plugin β Notification β Data β
β (JWT/Shiro) β System β Engine β Loader β
β β (30+ plugins)β β β
ββββββββββββββββββ΄βββββββββββββββββ΄βββββββββββββββββ΄ββββββββββββββββββββββββββ€
β EVENT BUS (lib/bus.js) β
β Stream-based pub/sub: tick, data-update, notification β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β DATA LAYER β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β MongoDB 3.6+ (via mongodb driver) β
β Collections: entries, treatments, devicestatus, profile, food, activity β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β EXTERNAL INTEGRATIONS β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Pushover β IFTTT Maker β Dexcom Share β Medtronic CareLink β Loop/OpenAPS β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
nightscout/
βββ lib/ # Core application code
β βββ server/ # Server initialization and core services
β β βββ server.js # Entry point
β β βββ bootevent.js # Boot sequence orchestration
β β βββ app.js # Express app configuration
β β βββ websocket.js # Legacy WebSocket handler
β β βββ pebble.js # Pebble watch API
β β βββ pushnotify.js # Push notification orchestration
β β βββ env.js # Environment configuration
β β
β βββ api/ # REST API v1 endpoints
β βββ api2/ # REST API v2 (authorization extensions)
β βββ api3/ # REST API v3 (OpenAPI 3.0 compliant)
β β βββ storageSocket.js # Real-time data broadcast
β β βββ alarmSocket.js # Real-time alarm broadcast
β β βββ security.js # API v3 security middleware
β β
β βββ authorization/ # Auth system (JWT, Shiro permissions)
β βββ plugins/ # 38 plugins (data processing, alarms, etc.)
β βββ client/ # Client-side JavaScript modules
β βββ data/ # Data loading and processing
β βββ storage/ # Database adapters (MongoDB, OpenAPS)
β βββ report_plugins/ # Report generation plugins
β βββ middleware/ # Express middleware
β β
β βββ bus.js # Internal event bus
β βββ notifications.js # Notification/alarm management
β βββ sandbox.js # Plugin execution sandbox
β βββ settings.js # Application settings
β
βββ bundle/ # Webpack client bundle source
βββ static/ # Static assets (CSS, JS, images)
βββ views/ # EJS templates and clock views
βββ tests/ # Mocha test suite
βββ docs/ # Documentation
βββ webpack/ # Webpack configuration
The application follows a sequential boot process using the bootevent library:
startBoot β checkNodeVersion β checkEnv β augmentSettings β checkSettings
β
setupStorage β setupAuthorization β setupInternals β ensureIndexes
β
setupListeners β setupConnect β setupBridge β setupMMConnect β finishBoot
Key Boot Tasks:
- startBoot: Initialize context (ctx), event bus, admin notifications
- setupStorage: Connect to MongoDB or OpenAPS storage
- setupAuthorization: Load JWT/Shiro authorization system
- setupInternals: Initialize plugins, data loaders, notifications
- setupListeners: Wire up event bus handlers for data processing
A lightweight Node.js Stream-based pub/sub system for internal communication.
Core Events:
| Event | Trigger | Subscribers |
|---|---|---|
tick |
Heartbeat interval | Data loader, plugins |
data-received |
New data ingested | Data loader |
data-loaded |
Data refresh complete | Plugin system, sandbox |
data-processed |
Plugins finished | Runtime state |
notification |
Alert triggered | Push notify, WebSocket |
teardown |
Server shutdown | All cleanup handlers |
Modernization Note: The Stream-based event bus is functional but dated. Consider migrating to EventEmitter3 or a typed event system for better debugging and TypeScript compatibility.
Extensible plugin architecture with 38 plugins for data processing, visualization, and alerting.
Plugin Types:
pill-primary: Primary display values (bgnow, rawbg)pill-status: Status indicators (timeago, upbat)forecast: Predictive algorithms (ar2)report: Historical analysis (dailystats, glucosedistribution)notification: Alert generators (simplealarms, treatmentnotify)
Plugin Lifecycle:
- Registration during boot
setProperties(): Calculate derived valuescheckNotifications(): Generate alertsupdateVisualisation(): Update UI elements
CGM Device β Uploader β REST API β MongoDB β Data Loader
β
Plugin Processing
β
Event Bus (data-processed)
β
ββββββββββββββββββββ΄βββββββββββββββββββ
β β
WebSocket Broadcast Push Notifications
(Dashboard Update) (Pushover/IFTTT)
| Package | Version | Purpose | Modernization Notes |
|---|---|---|---|
| express | 4.17.1 | Web framework | Update to 4.18+ or 5.x |
| mongodb | ^3.6.0 | Database driver | Update to 4.x+ for better types |
| socket.io | ~4.5.4 | Real-time comms | Current (good) |
| jsonwebtoken | ^9.0.0 | JWT handling | Current (good) |
| shiro-trie | ^0.4.9 | Permission model | Unique, consider alternatives |
| moment | ^2.27.0 | Date handling | Consider dayjs or Temporal |
| lodash | ^4.17.20 | Utilities | Current, consider tree-shaking |
| request | ^2.88.2 | HTTP client | DEPRECATED - migrate to axios |
| Package | Version | Purpose | Modernization Notes |
|---|---|---|---|
| jquery | ^3.5.1 | DOM manipulation | Consider modern alternatives |
| d3 | ^5.16.0 | Data visualization | Update to D3 v7 |
| flot | ^0.8.3 | Legacy charting | Consider Chart.js or D3-only |
| webpack | ^5.74.0 | Bundling | Current (good) |
| Integration | Purpose | Notes |
|---|---|---|
| Pushover | Push notifications | Paid service, callback support |
| IFTTT Maker | Webhook automation | Event-based triggers |
| Dexcom Share | CGM data bridge | Deprecated in favor of nightscout-connect |
| Medtronic CareLink | CGM data bridge | Deprecated in favor of nightscout-connect |
| Alexa | Voice assistant | Custom skill support |
| Google Home | Voice assistant | Custom actions support |
| Version | Base Path | Auth Method | Status |
|---|---|---|---|
| v1 | /api/v1 |
API_SECRET header/query | Legacy, widely used |
| v2 | /api/v2 |
JWT tokens | Current default |
| v3 | /api/v3 |
JWT tokens, OpenAPI 3.0 | Modern, recommended |
v1 Endpoints:
/entries- Glucose readings (SGV data)/treatments- Treatment events (insulin, carbs, notes)/profile- User profiles and settings/devicestatus- Device/loop status/food- Food database/status- Server status
v2 Extensions:
/authorization- Token management/properties- System properties/ddata- Aggregated data endpoint
v3 Generic Collections:
/{collection}- CRUD for all collections/{collection}/history/{lastModified}- Incremental sync/version,/status,/lastModified- Metadata
| Namespace | Purpose | Auth Required |
|---|---|---|
/ (default) |
Legacy data updates | API_SECRET or token |
/storage |
Collection CRUD events | accessToken |
/alarm |
Alarm/announcement broadcast | accessToken |
Storage Events:
create- Document createdupdate- Document modifieddelete- Document removed
Alarm Events:
announcement- User announcementalarm- Standard alarm (WARN level)urgent_alarm- Urgent alarm (URGENT level)clear_alarm- Alarm cleared
Current:
- API_SECRET: SHA-1 hash comparison for admin access
- Access Tokens: Pre-shared tokens for subjects
- JWT: Signed tokens with expiration
Planned (OIDC/OAuth2 Plugin): 4. OIDC/OAuth2: Vendor-agnostic identity via external providers
- Integration with Ory Hydra/Kratos for consent management
- nightscout-roles-gateway for delegation and data rights
- Claims mapped to Shiro permissions
- Verified actor identity for all data mutations
- See OIDC Actor Identity Proposal for implementation details
Uses Apache Shiro-style permissions:
api:entries:read # Read entries collection
api:treatments:create # Create treatments
* # Admin (all permissions)
Permission Hierarchy:
Subject β Roles β Permissions β Shiro Trie (check access)
Authority Model (Control Plane RFC):
Human > Agent > Controller
Location: lib/authorization/delaylist.js
IP-based progressive delay for failed authentication attempts:
- Configurable delay via
authFailDelaysetting (default 5000ms) - Cumulative delays per IP address
- Auto-clears after 60 seconds of inactivity
Note: General API rate limiting is not currently implemented.
| Issue | Severity | Location | Recommendation |
|---|---|---|---|
Deprecated request library |
High | Multiple files | Migrate to axios |
| Legacy callback patterns | Medium | Storage, auth | Async/await refactor |
| jQuery DOM manipulation | Medium | Client code | Modern framework |
| Mixed CommonJS/ES modules | Low | Bundle | Standardize on ES modules |
| Moment.js bundle size | Low | Client bundle | Replace with dayjs |
| Inconsistent error handling | Medium | API layers | Unified error middleware |
- Single-threaded: No clustering support out of box
- In-memory state: Notifications, alarms stored in memory
- Poll-based updates: Heartbeat-driven data loading
- Large client bundle: ~1MB+ JavaScript payload
- No TypeScript: Pure JavaScript with JSDoc
- Tight coupling: Plugins tightly coupled to sandbox
- Global state: Extensive use of shared
ctxobject - Test coverage: Limited automated testing
- Replace deprecated
requestlibrary with axios - Add general API rate limiting (express-rate-limit)
- Add input validation middleware (Zod/Joi)
- Implement structured logging (pino)
- Add TypeScript definitions for core modules
- Convert callbacks to async/await
- Implement database migrations (instead of ensureIndexes)
- Expand test coverage
- OIDC/OAuth2 Plugin: Vendor-agnostic identity integration
- See OIDC Actor Identity Proposal for full RFC
- nightscout-roles-gateway: Consent and delegation management
- Ory Hydra/Kratos: Identity backend for multi-user deployments
- Actor Identity: Replace freeform
enteredBywith verified actor claims - Maintain backward compatibility with API_SECRET auth
- Bundle optimization (replace Moment.js, tree-shake lodash)
- PWA support (service worker, manifest)
- Migrate jQuery to vanilla JS or modern framework
- Accessibility improvements