CodingNic

Backend Services

Module Overview

Backend Services 5 min read

Module Overview

Backend Services

This module builds the whole SKYWATCH backend: a Fastify API server with a route → repo layering pattern every later resource reuses, a background poller that fetches live aircraft data from adsb.lol on a fixed interval, an edge-triggered detection engine that catches emergency squawks and “aircraft near home” overflights, and the real-time layer that pushes all of it to connected browsers over WebSocket.

Why This Matters

Three design decisions in this module show up everywhere else in the course. First, route handlers never touch a Drizzle table directly — they call a repo function and shape the response. That separation is what lets a repo function grow real transactional logic (like keeping exactly one watch region marked default) without the route file that calls it ever changing. Second, detection is edge-triggered: it keeps small in-memory state and only emits a detection the moment something transitions from not-alerting to alerting, not once per poll cycle for as long as it stays true. Without that, one ten-minute emergency squawk would generate dozens of duplicate alerts instead of one. Third, positions and alerts travel over two genuinely different pub/sub mechanisms in the same server — an in-process EventEmitter for positions, real Postgres LISTEN/NOTIFY for alerts — because a busy region’s aircraft data blows past Postgres’s 8000-byte NOTIFY payload limit, while alerts are small, rare, and benefit from not being tied to a single process.

What You’ll Learn

  • Standing up a Fastify server with environment config, CORS, a health check, and graceful shutdown
  • The route → repo layering pattern, and building a full CRUD resource (watch regions) on top of it, including transactional writes
  • Fetching live aircraft data from adsb.lol on a fixed interval with a re-entrancy guard and per-region error isolation
  • Edge-triggered detection for emergency squawks and geofenced overflights
  • Why positions and alerts use two different pub/sub mechanisms, and the one Postgres connection-pooling mistake that silently breaks LISTEN
  • Pushing positions and alerts to the browser over a single WebSocket endpoint

Outcome

By the end of this module, you’ll have a backend service running standalone — polling live aircraft data, detecting emergency squawks and overflights, and pushing all of it to any connected browser over WebSocket in real time.

Let’s get started.