Appearance
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:
mqttpackage 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)
- Generate JWT RS256 key pair
- Start SQLite databases (main + sensor)
- Run pending migrations
- Start HTTP + WebSocket server
- Start MQTT client
- Initialize beacon cache
- Start tracking prediction
- Start device sync
"bridge" mode (standalone)
- Start MQTT client
- Forward beacon/gateway data to Zabbix
Both modes can run simultaneously when server_features includes both "bridge" and "safecall".
Module Overview
| Module | File | Purpose |
|---|---|---|
| HTTP Routes | src/routes/http.ts | All REST API endpoints |
| WebSocket | src/routes/ws.ts | Real-time subscriptions |
| MQTT | src/mqtt.ts | MQTT message dispatcher |
| Tracking | src/tracking.ts | Zone prediction algorithms |
| Events | src/events.ts | Panic buttons, SOS, sensor alerts |
| Cache | src/cache.ts | In-memory beacon state |
| Devices | src/devices.ts | Device sync to database |
| Bridge | src/bridge.ts | Zabbix/LLD integration |
| Guard | src/guard.ts | JWT auth middleware |
| Schemas | src/schemas.ts | Valibot 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
| Package | Version | Purpose |
|---|---|---|
jose | ^6.0.10 | JWT signing and verification |
mqtt | ^5.12.0 | MQTT broker client |
protobufjs | ^7.5.4 | Tracking log serialization |
valibot | ^1.1.0 | Runtime 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