How did SkyAlertsApp port from Next.js and Neon Postgres to Flutter and Firebase?

Answer

SkyAlertsApp replaced Next.js, Vercel, and Neon Postgres 18 with one Flutter client backed by Firebase Hosting, Cloud Firestore (nam5), Firebase Auth, FCM, and 11 Python Cloud Functions.1

  1. One codebase across platforms: Flutter Web, iOS, and Android share instant splash rendering, on-device visibility math, 9 Solar System portraits, and OLED Red Night Mode.2
  2. One-read Firestore rollups: Nightly jobs pre-aggregate rollups/overview and rollups/trends_90d alongside objects/{objectId}, cutting dashboard startup from SQL joins to two document reads.34
  3. Bounded Gen-2 execution: Cloud Scheduler triggers Python 3.12 Gen-2 Functions with hard ingest caps (360 s budget, 5 pages per source, 60 enriched objects) inside the 540 s timeout.56
  4. Same-origin Agent & Pro APIs: Firebase Hosting rewrites /mcp, /api/**, and /webhooks/revenuecat to Python Gen-2 HTTP endpoints while browser WebMCP tools run in Dart.78

Applies toSkyAlertsAppcommit dbb2656Flutter + Firebase (skyalerts-app)

1

What changed from the legacy Next.js and Postgres stack?

Every major runtime layer moved from server-rendered Next.js on Vercel into client-rendered Flutter and Google Cloud serverless primitives under skyalerts-app. Only shared Python domain helpers in ingest/ and digest/ remained intact inside the new Gen-2 Functions bundle.

LayerLegacy Stack (web/)Current Stack (app/ and functions/)
Client UIWeb onlyNext.js App Router (Web only)Cross-platformFlutter 3 (Web, iOS, Android)
Edge & HostingVercel Hosting + next.config.tsCloudflare + Firebase Hosting (app/build/web)
Primary DatabasePooled TCPNeon Postgres 18 (sql migrations)Offline syncCloud Firestore nam5 (NoSQL + security rules)
Scheduled IngestVercel Cron / scriptsCloud Scheduler (512 MiB / 540 s Gen-2)
Auth & AdminGitHub OAuth + HTTP cookie sessionsAnonymous-first Firebase Auth + Google/Apple + claim guard
Backyard & PushStatic views, no push channelOn-device visibility math + Open-Meteo + FCM pushes
AI & AutomationStatic explainer HTML onlyBrowser WebMCP + /mcp + /api/v1/* + RevenueCat webhook
2

How do Cloudflare, Firebase Hosting, Flutter, and Cloud Functions connect?

SkyAlerts Firebase & Cloudflare Topology
Diagram structure
  • Flutter Web / Mobile client
  • Cloudflare Edge cf
  • Firebase Hosting hosting
  • Firebase Auth auth
  • Cloud Firestore nam5 db
  • Cloud Scheduler sched
  • Daily Ingest Fn ingestfn
  • Pro API & MCP Fn apifn
  • FCM & Resend push
  • ALeRCE / SkyBoT / JPL astro
  • Open-Meteo / 7Timer meteo
  • client → cf · HTTPS
  • cf → hosting · Static & rewrites
  • client → auth · Sign in & link
  • client → db · SDK queries
  • client → meteo · 0.1-deg weather
  • hosting → apifn · "/mcp, /api, webhook"
  • sched → ingestfn · UTC cron
  • ingestfn → astro · Survey & ephemeris
  • ingestfn → db · Objects & rollups
  • ingestfn → meteo · Clear-sky check
  • ingestfn → push · Alerts & digest
  • apifn → db · Keys & catalog
  • apifn → meteo · Cloud cover

Cloudflare terminates TLS and caches immutable assets in front of Firebase Hosting, which serves the Flutter bundle and proxies dynamic API and webhook paths to Cloud Functions. Reads go directly from Flutter clients to Cloud Firestore under declarative Security Rules.910

  1. Flutter Web / Mobile

    Boots via an instant #app-splash shell, attaches WebMCP on navigator.modelContext, and syncs entitlements/{uid} for Pro state.

  2. Firebase Hosting

    Applies global CSP ('wasm-unsafe-eval'), COOP (same-origin-allow-popups), immutable asset caching, and rewrites /mcp, /api/**, and /webhooks/revenuecat.

  3. Cloud Firestore nam5

    Serves public objects and rollups, owner-scoped users/{uid} and entitlements/{uid}, and server-only api_keys/{keyHash}.

  4. Daily Ingest Fn

    Runs daily at 06:17 UTC inside 540 s to fetch surveys, SkyBoT matches, bright targets, Horizons ephemerides, and Pro backyard notifications.

  5. Pro API & MCP Fn

    Authenticates ska_live_... API keys or Firebase ID tokens via SHA-256 lookup before executing REST or MCP JSON-RPC 2.0 tools.

3

How does the daily pipeline stay inside the 540 s Gen-2 timeout?

Bounded Daily Ingest & Alert Pipeline
Diagram structure
  • 06:17 UTC Cron cron
  • ALeRCE Streams alerce
  • IMCCE SkyBoT skybot
  • Bright & Comets bright
  • JPL Horizons hor
  • Save Rollups rol
  • Dispatch Pushes fcm
  • alerce → skybot · Bounded batch
  • bright → hor · Touch objects
  • cron → alerce · Start run
  • hor → rol · 9 bodies
  • rol → fcm · Ready
  • skybot → bright · Matched SSOs

Cloud Functions Gen 2 caps HTTP and scheduled work at 540 s, so execute_daily_pipeline enforces a strict 360 s ingest budget before starting downstream stages. The remaining 180 s window handles asteroid identification, bright targets, JPL Horizons fetches, rollup writes, and FCM push fan-out.611

  1. 06:17 UTC Cron

    Cloud Scheduler invokes daily_ingest_and_identify (512 MiB, 540 s) in UTC.

  2. ALeRCE Streams

    Pages LSST and ZTF candidates up to 5 pages per source, 60 enriched objects, and 360 s total ingest budget.

  3. IMCCE SkyBoT

    Cross-matches unflagged moving detections against SkyBoT asteroid fields and attaches sso metadata.

  4. Bright & Comets

    Ingests Rochester/TNS supernovae, JPL NEO Close Approaches, MPC comets, and evergreen variables for amateur apertures.

  5. JPL Horizons

    Fetches nightly J2000 coordinates, V magnitudes, AU distances, and solar elongations for 9 outer dwarf planets and candidates.

  6. Save Rollups

    Writes rollups/overview and rollups/trends_90d so app tabs load without collection scans.

  7. Dispatch Pushes

    Sends transient, bookmark, asteroid, and entitlements-verified Pro clear-sky backyard alerts via FCM.

Daily schedule
17 6 * * * UTC (512 MiB, 540 s timeout)
Weekly digest schedule
23 14 * * 0 UTC (256 MiB, 180 s timeout)
Per-source page cap
max_pages_per_source = 5
Ingest time budget
time_budget_s = 360.0 (leaves 180 s headroom)
Photometry enrichment cap
enrich_max_objects = 60 per run
4

How is Cloud Firestore modeled and locked down by Security Rules?

Firestore separates public astronomy data from owner-scoped user state and server-only secrets across eight collections. Client apps never write catalog objects, Pro entitlements, or hashed API keys directly.1012

Firestore Collections & Access Ownership
Diagram structure
  • objects/{id} objects
  • detail/enrichment detail
  • rollups/{docId} rollups
  • users/{uid} users
  • users/{uid}/bookmarks bookmarks
  • entitlements/{uid} entitlements
  • api_keys/{keyHash} api_keys
  • users/{uid}/api_keys user_api_keys
  • detail → objects · enriches
  • bookmarks → users · follows
  • entitlements → users · Pro access
  • api_keys → users · authenticates
  • user_api_keys → users · lists
CollectionClient ReadClient WriteAccess & Schema Guardrail
objects/{objectId} & detailPublicDeniedServer Admin SDK only; 1 catalog doc + 1 enrichment doc
rollups/{docId}PublicDeniedPre-aggregated overview and trends_90d
users/{uid}Owner onlyOwner validatedEnforces allowed keys, min_confidence range, max 10 FCM tokens
users/{uid}/bookmarksOwner onlyOwner validatedEnum survey check; immutable object_key and created_at
users/{uid}/api_keysOwner onlyDeniedNon-secret metadata (prefix, label, revoked)
entitlements/{uid}Owner onlyDeniedSynced only by revenuecat_webhook / Admin SDK
api_keys/{keyHash}DeniedDeniedSHA-256 lookup exclusively inside Cloud Functions
admin_runs & audit_logAdmin claim / ownerDeniedVerified admin == true or verified owner email
5

How do WebMCP, Remote MCP, and Pro API keys work with 0.1-degree privacy?

Browser AI agents call free read-only tools (skyalerts_get_tonight_targets, skyalerts_get_solar_system, skyalerts_search_catalog) registered on navigator.modelContext, while external MCP clients and scripts call Pro endpoints at /mcp and /api/v1/* through Firebase Hosting rewrites.87

Pro Remote MCP & REST Request Lifecycle
Diagram structure
  • AI / N.I.N.A. Client agent
  • Firebase Hosting hosting
  • skyalerts_api Fn api
  • Cloud Firestore db
  • Open-Meteo API meteo
  • agent → hosting · GET /mcp (or POST tools/list)
  • hosting → api · Rewrite /mcp to Gen-2
  • api → agent · Tool schemas + protocol 2025-03-26
  • agent → hosting · POST /mcp tools/call + Bearer ska_live_...
  • hosting → api · Forward request headers & body
  • api → db · Lookup api_keys/{sha256} & entitlements/{uid}
  • db → api · Valid key + active Pro status
  • api → meteo · GET cloud_cover at rounded lat/lon (0.1 deg)
  • meteo → api · Hourly cloud cover %
  • api → db · Read objects & rollups/overview
  • db → api · Targets & ephemerides
  • api → agent · Ranked J2000 JSON / N.I.N.A. CSV
6

What 11 Cloud Functions and deploy guardrails run in production?

All serverless work lives inside one functions/ codebase running Python 3.12 Gen-2 Functions with per-function memory and timeout isolation.513

Function NameTriggerMemory / TimeoutResponsibility
daily_ingest_and_identifyon_schedule (17 6 * * * UTC)512 MiB / 540 sIngests ALeRCE, SkyBoT, bright targets, Horizons, pushes
weekly_digest_and_broadcaston_schedule (23 14 * * 0 UTC)256 MiB / 180 sBuilds weekly dispatch markdown, audio script, email, FCM
trigger_ingeston_call (Admin + audit)512 MiB / 540 sOn-demand daily pipeline rerun for LSST/ZTF
trigger_identifyon_call (Admin + audit)256 MiB / 180 sManual SkyBoT pass + rollup recompute
preview_digest / send_digeston_call (Admin + audit)256 MiB / 120–180 sDry-run preview or live Resend + FCM broadcast
create_pro_api_keyon_call (Pro / Owner)256 MiB / 30 sIssues salted ska_live_... key; stores SHA-256 hash
list_pro_api_keys / revoke_pro_api_keyon_call (Signed-in user)256 MiB / 30 sLists non-secret key metadata or revokes immediately
skyalerts_apion_request (/mcp, /api/**)512 MiB / 60 sServes /api/v1/health, REST JSON, and Streamable MCP
revenuecat_webhookon_request (/webhooks/revenuecat)256 MiB / 30 sSyncs RevenueCat billing events into entitlements/{uid}

Opening diagram…