island-mode-server
Skill0 starsChanged 2 months agoMITView on GitHub
Build an on-premise fallback server that keeps a live two-way RxDB replica of a site's slice of Firestore, takes over serving LAN terminals when the cloud is unreachable, and flushes offline work back on reconnect. Use when: (1) a physical site (store, gym, clinic, warehouse) must keep taking bookings, moving stock and toggling hardware through an internet outage, (2) kiosk or staff PWAs must switch between a cloud API and a local API on their own, (3) an existing offline server needs auditing: a reconnect flush that double-counts stock, a replica that stops pulling, a failover that never finds the box, (4) the user mentions: island mode, offline fallback server, LAN failover, on-prem replica, local server with two-way sync to Firestore, "the site keeps working when the internet is down", replicateFirestore, rxdb/plugins/replication-firestore, serverTimestamp checkpoint, _offlineCreated, _locallyModified, syncedIds, FieldValue.increment, heartbeat cron, apiBaseUrl, HMAC device auth, island.local, avahi. Carries the three replication tiers, the checkpoint stamp every cloud write needs, the per-ID stock-delta fold-out, idempotent ingestion keyed on client-generated IDs, the offline-gated staff token fallback, the heartbeat and status chain, and a 17-test vitest suite for the trust-critical logic. Node/NestJS local server and Firestore cloud with Next.js route handlers as the reference API; the HTTP framework, IdP and vocabulary are seams in architecture.md. Not a read cache, not multi-master sync between sites, and not for a cloud database other than Firestore.
- Reads credentials
In this repositoryOne other file it gives its agents.
- CLAUDE.mdCLAUDE.md
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 130
Add the reconnect flush and idempotent cloud ingestion: sync-flush.md.
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Island-Mode Server
A small Node server runs at the physical site holding a live RxDB replica of the site's slice of Firestore. While the internet is up it is invisible; when the internet drops, kiosk and staff terminals on the LAN fail over to it and the site keeps taking orders, moving stock and toggling hardware. On reconnect, offline work flushes back to the cloud. The one idea the design turns on: sync is two systems, not one. Document-level RxDB replication keeps state current, and an HTTP flush of business events lets the cloud apply them with its own logic (stock increments, audit ingestion). Neither alone is sufficient.
When to use
- A site must survive internet outages with real writes (bookings, stock, hardware control), not just cached reads.
- Terminals are browser-based (PWA or kiosk) and must fail over transparently.
- Firestore is the cloud source of truth and stays that way.
When NOT to use
- Pure read caching, or a PWA that only needs Firestore's built-in offline persistence: the Firebase SDK already does that, with no server.
- A different cloud database (Supabase, Postgres): the replication tier concept travels, but every template here is Firestore-specific.
- Multi-master sync between peer sites: this design is strictly hub-and-spoke, with the cloud as source of truth and last-write-wins conflicts.
Architecture
Internet
|
+--------+---------+
| Cloud (SSoT) | Firestore + public API + cron status marker
+--------+---------+
| |
RxDB replication HTTP flush (reconnect)
| |
+-+-------------+-+
| Local server | Node/NestJS + RxDB, LAN :443 via nginx TLS
+--------+--------+
|
LAN: kiosk PWA, staff PWA, hardware controllersThe seam contract with the host lives in
architecture.md (Adaptation contract): the
rename table from location / booking / inventory / lock / staff / pricing,
the tenant field, the cloud API framework, the IdP and the hardware auth.
Critical facts
- Every cloud write to a replicated collection stamps the checkpoint
field and
_deleted: false. The RxDB Firestore plugin pulls byserverTimestamp > checkpoint, so an unstamped document is never pulled. - Each collection gets one of three replication tiers. Pull-only for config the site consumes, bidirectional for operational state the site mutates, push-only for logs the site produces; the wrong tier for a collection is the main design error.
- In-memory RxDB storage loses all offline work on an offline restart. The replica repopulates from Firestore, but offline-created documents are gone, so the storage trade-off is decided explicitly in replication.md.
- Cloud ingestion is idempotent. The flush is at-least-once, so every apply is keyed on the client-generated ID and checks a receipt only the ingestion writes, never the document replication pushes.
- Bidirectional pushes carry only local changes. The push filter sends
documents flagged
_offlineCreatedor_locallyModified, so pulled cloud documents do not bounce back. - Terminals prefer the cloud. Failover engages only after N consecutive health failures, and while offline they keep rescanning for the local server on every tick, not once at the transition.
Hard rules
Never expose the cloud sync-ingestion endpoints without auth. They apply stock increments and inject orders. A shared-secret header is the minimum.
Never verify offline staff tokens leniently while online. The decode-without-verification fallback is an accepted LAN-only trade-off; it must be gated on the server actually being offline, or it becomes a bypass of signature verification.
Never reset local stock deltas for transactions that have not confirmed as synced. Reset per ID on acknowledgment, or offline sales double-count or vanish from availability.
Never let the local server invent business rules the cloud owns (opening hours, pricing). Replicate the config and compute from it, or island-mode behaviour diverges from the website.
Quick start
Copy each code block as written to the path on its first line; renames, imports and seams are the only edits. Variants and extra hardening go in new files around a template; a suspected defect goes in the handover.
- Model collections into tiers and name the seams: architecture.md.
- Stand up RxDB and replication with custom-token auth and security rules: replication.md.
- Add the reconnect flush and idempotent cloud ingestion: sync-flush.md.
- Wire the heartbeat and status chain and client failover: network-failover.md.
- Guard the local API and mirror the endpoints terminals need: auth.md, local-api.md.
- Deploy on site: both env files keep every name in operations.md
(
GOOGLE_APPLICATION_CREDENTIALSis a path read from the environment, not to be inlined;LOCATION_IDrequired), systemd, nginx TLS, mDNS, CORS: operations.md. - Carry behavior.test.ts into
local-server/src/, point its../next/imports atlib/island/andlib/sync/, install vitest, and run it unmodified: 17 tests. - Hand over the storage loss window, the
replicationStamp()audit of the host's own writes, the mirrored secrets, and the terminal CA and CORS: operations.md (Handover).
Reference directory
| Scenario | Trigger keywords | Reference |
| Topology, tiers, seams, rename table | pull-only, bidirectional, push-only, dual-path, locationId, conflict | architecture.md |
| Replica setup, schemas, checkpoint trap | RxDB, replicateFirestore, serverTimestamp, custom token, storage-memory, firestore.rules | replication.md |
| Reconnect flush, stock deltas, ingestion | flushAll, idempotent, receipt, FieldValue.increment, localDelta, syncedIds | sync-flush.md |
| Outage detection and API switching | heartbeat, lastHeartbeatAt, cron, NetworkManager, apiBaseUrl, failover | network-failover.md |
| Local API auth | AuthGuard, HMAC, replay window, offline JWT fallback, kiosk key | auth.md |
| Offline endpoints and writes | availability, mutex, offline booking, check-in, pricing stock filter | local-api.md |
| On-site deployment, env and handover | systemd, nginx, self-signed TLS, mDNS, avahi, CORS_ORIGINS, .env.example, handover, rollback | operations.md |
| The ledger: what the audit changed, kept, added | provenance, audit, deviations, kept deliberately | provenance.md |
| Regression cover for the trust-critical logic | vitest, verifyHmac, decodeOfflineToken, delta fold-out, ingestion receipt, opening hours, rescan | behavior.test.ts |
Part of the Timerise Skills index, which lists the sibling skills.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

