Skip to content

Server

The SafeCall server is a Bun-powered backend that provides the HTTP/WebSocket API, MQTT integration, real-time beacon tracking, and optional Zabbix bridge functionality.

Tech Stack

  • Runtime: Bun
  • Language: TypeScript
  • Database: SQLite (via bun:sqlite)
  • Auth: JWT with RS256 (via jose)
  • MQTT: mqtt package for broker communication
  • Validation: Valibot for schema validation
  • Serialization: Protobuf (via protobufjs) for tracking logs

Entry Point

The server starts from server/src/server.ts. It reads main.config.json and initializes subsystems based on the server_features config:

"safecall" mode (full server)

  1. Generate JWT RS256 key pair
  2. Start SQLite databases (main + sensor)
  3. Run pending migrations
  4. Start HTTP + WebSocket server
  5. Start MQTT client
  6. Initialize beacon cache
  7. Start tracking prediction
  8. Start device sync

"bridge" mode (standalone)

  1. Start MQTT client
  2. Forward beacon/gateway data to Zabbix

Both modes can run simultaneously when server_features includes both "bridge" and "safecall".

Module Overview

ModuleFilePurpose
HTTP Routessrc/routes/http.tsAll REST API endpoints
WebSocketsrc/routes/ws.tsReal-time subscriptions
MQTTsrc/mqtt.tsMQTT message dispatcher
Trackingsrc/tracking.tsZone prediction algorithms
Eventssrc/events.tsPanic buttons, SOS, sensor alerts
Cachesrc/cache.tsIn-memory beacon state
Devicessrc/devices.tsDevice sync to database
Bridgesrc/bridge.tsZabbix/LLD integration
Guardsrc/guard.tsJWT auth middleware
Schemassrc/schemas.tsValibot schemas for DB rows

Data Flow

MQTT Broker


mqtt.ts (dispatcher)
    ├── bridge.ts → Zabbix
    ├── devices.ts → DB sync
    └── events.ts
            ├── cache.ts (beacon lookup)
            ├── tracking.ts (RSSI → zone prediction)
            └── KISS / ntfy.sh (notifications)

WebSocket Clients


ws.ts (subscribe by type)
    └── events.ts (test beacon simulation)

HTTP Clients


routes/http.ts
    └── guard.ts (JWT verification)

Key Dependencies

PackageVersionPurpose
jose^6.0.10JWT signing and verification
mqtt^5.12.0MQTT broker client
protobufjs^7.5.4Tracking log serialization
valibot^1.1.0Runtime schema validation

Development

bash
cd server

# Install dependencies
bun install

# Run dev server with hot reload
bun run dev

# Type checking
bun run typecheck

# Run tests
bun test

# Run tests with watch
bun test --watch