Cookbook · Node.js 18+
Earthquake alert ahead of the wave
Satelit detects deep Vrancea earthquakes by itself, in their first seconds. The wave that shakes needs about a minute to reach Chișinău. In between, the earthquake.early webhook reaches your server. Replayed on 84 real earthquakes since 2014: every one of magnitude 5 and more was alerted 17–24 seconds after it started, 35–44 seconds ahead of the S wave in Chișinău.
- Runs on
- Node.js 18+
- One run
- 0 credits
- A month
- 0 credits
- Set up in
- 15 min
$ CITY=chisinau MIN_INTENSITY=3 PORT=8080 node quake_alert.mjsEARTHQUAKE ALERT: Vrancea, M 5.5 (estimate, +-0.28). S wave in chisinau in 44 s, estimated intensity IV.How it works
- Add your server's address in the dashboard with the events
earthquake.earlyandearthquake. The first needs the Pro plan or higher. - The script checks the signature in
Satelit-Signatureand answers 200 before anything else. - It takes your town from
data.moldovaand works out the seconds left froms_wave_at, not from the moment of sending. - It tells the channel at once, then again at
stage: 2with the better estimate. The located earthquake follows asearthquake.
What it costs: Webhooks spend no credits. earthquake.early is on the Pro, Business and Enterprise plans; earthquake, after location, on any plan with webhooks. The Start plan has no webhooks.
How to run it
export SATELIT_WEBHOOK_SECRET=whsec_...
export TELEGRAM_TOKEN=... TELEGRAM_CHAT=@canalul_tau
CITY=chisinau MIN_INTENSITY=3 PORT=8080 node quake_alert.mjsWhat it outputs
EARTHQUAKE ALERT: Vrancea, M 5.5 (estimate, +-0.28). S wave in chisinau in 44 s, estimated intensity IV.The full script
#!/usr/bin/env node
// Receives earthquake.early from Satelit (Pro, Business, Enterprise): a deep Vrancea earthquake seen about 20 seconds
// after it starts, tens of seconds before its S wave reaches Moldova. Checks the signature, works out the seconds left
// for your town and tells your channel at once. The located earthquake follows as `earthquake`.
// SATELIT_WEBHOOK_SECRET=... CITY=chisinau MIN_INTENSITY=3 PORT=8080 node quake_alert.mjs
// TELEGRAM_TOKEN=... TELEGRAM_CHAT=@your_channel (without them the alert is printed)
// Put it behind HTTPS and add the address in the dashboard with the events earthquake.early and earthquake.
import { createHmac, timingSafeEqual } from "node:crypto";
import { createServer } from "node:http";
const SECRET = process.env.SATELIT_WEBHOOK_SECRET;
if (!SECRET) throw new Error("Set SATELIT_WEBHOOK_SECRET");
const CITY = process.env.CITY ?? "chisinau";
const MIN_INTENSITY = Number(process.env.MIN_INTENSITY ?? 3);
const { TELEGRAM_TOKEN, TELEGRAM_CHAT } = process.env;
const seen = new Set();
function verified(rawBody, header = "") {
const parts = Object.fromEntries(header.split(",").map((p) => p.split("=")));
const t = Number(parts.t);
if (!t || Math.abs(Date.now() / 1000 - t) > 300) return false;
const expected = Buffer.from(createHmac("sha256", SECRET).update(`${t}.${rawBody}`).digest("hex"));
const given = Buffer.from(parts.v1 ?? "");
return expected.length === given.length && timingSafeEqual(expected, given);
}
async function tell(text) {
console.log(text);
if (!TELEGRAM_TOKEN || !TELEGRAM_CHAT) return;
// no waiting on Telegram's answer: every second counts, and a failed post must not stop the next alert
fetch(`https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ chat_id: TELEGRAM_CHAT, text }),
}).catch((e) => console.error(`telegram: ${e.message}`));
}
function early(data) {
const town = data.moldova.find((t) => t.key === CITY) ?? data.strongest;
if (town.intensity_estimate < MIN_INTENSITY) return;
// the seconds left now, from the wave's own arrival time: the event took a moment to reach us
const left = Math.max(0, Math.round((new Date(town.s_wave_at) - Date.now()) / 1000));
const head = data.stage === 1 ? "EARTHQUAKE ALERT" : "Earthquake alert, updated";
tell(`${head}: Vrancea, M ${data.magnitude_estimate} (estimate, +-${data.magnitude_sd}). ` +
`S wave in ${town.key} in ${left} s, estimated intensity ${town.intensity_roman}. Preliminary. ${data.attribution}`);
}
function located(data) {
tell(`Earthquake located: M ${data.magnitude} ${data.region}, depth ${data.depth_km} km. ` +
`Estimated in ${data.moldova.strongest_in_name}: ${data.moldova.intensity_roman}. ${data.url}`);
}
const server = createServer((req, res) => {
const chunks = [];
req.on("data", (c) => chunks.push(c));
req.on("end", () => {
const raw = Buffer.concat(chunks).toString("utf8");
if (!verified(raw, req.headers["satelit-signature"])) {
res.writeHead(401).end();
return;
}
const event = JSON.parse(raw);
res.writeHead(200).end(); // answer first: Satelit does not wait for your work
if (seen.has(event.id)) return; // a retry of what we already handled
seen.add(event.id);
if (event.type === "earthquake.early") early(event.data);
else if (event.type === "earthquake") located(event.data);
});
});
server.listen(Number(process.env.PORT ?? 8080), () => console.log(`listening on ${server.address().port}`));
Routes used
Questions about this cookbook
How early does the alert come?
About 20 seconds after a deep Vrancea earthquake starts. The S wave reaches Cahul after about 46 seconds and Chișinău after 57–66, so about 24 and 35–44 seconds are left.
How reliable is the magnitude in the alert?
It is an estimate from the earthquake's first seconds, with a standard deviation of 0.24–0.28. Above magnitude 6 it is a floor: the first seconds of a great earthquake do not show its whole size. The preliminary field is always true.
Which earthquakes are not alerted early?
Those that are not deep and from Vrancea: a crustal earthquake or one from another zone comes only as earthquake, after location. It is not an official warning system.