Status Indicators
elements man recipes/status-indicatorsA status page that shows a colored dot per monitored service ("API", "database",
"email", "cdn"). A health-check process (a cron job, a webhook handler, an admin
form) calls a setStatus RPC; the RPC broadcasts on a Channel. Every browser
on the status page receives the broadcast and re-colors the dot for the matching
service. No table, no LiveTable. The latest broadcast wins, and the page
reflects it.
This is a pure-channel pattern. Status is ephemeral state: clients care about the current value, not the history. The recipe lives entirely in one page directory plus the channel definition.
Page setup
Terminalelements create page status
The service names, the status union, the channel, and the setStatus RPC are
used only by the /status page (and whatever external triggers call the RPC),
so they live in the page's template.ehtml. The route imports them from there
and hands a listener to the template.
app/pages/status/index.tsimport { Request, Response } from "@elements/app"; import status, { statusChannel, SERVICES } from "./template"; export default function route(req: Request, res: Response) { return new status({ services: SERVICES, listener: statusChannel.listen(), }); }
The SERVICES constant is the list of monitored services. The channel carries
StatusEvent payloads: a service name and its new status. setStatus is the
only RPC; it's the universal entry point that whatever runs health checks (a
cron, a webhook, manual admin action) calls when a status changes.
The route returns the list of services plus a listener attached to the status
channel. The first paint shows every service with unknown status; the listener
updates them in place as broadcasts arrive.
app/pages/status/template.ehtmlimport "./style.css"; import { Channel, type Listener } from "@elements/app"; import { Badge, type BadgeVariant } from "@elements/ui/badge"; import { Item, ItemGroup, ItemContent, ItemTitle, ItemActions } from "@elements/ui/item"; export type ServiceName = "api" | "database" | "email" | "cdn"; export type Status = "ok" | "degraded" | "down" | "unknown"; export interface StatusEvent { name: ServiceName; status: Status; } export const SERVICES: ServiceName[] = ["api", "database", "email", "cdn"]; export const statusChannel = new Channel<StatusEvent>("service-status"); /** The badge color for each status. */ const VARIANTS: Record<Status, BadgeVariant> = { ok: "success", degraded: "warning", down: "danger", unknown: "secondary", }; /** @rpc */ export function setStatus(name: ServiceName, status: Status) { statusChannel.notify({ name, status }); } function onIncoming(event: StatusEvent, statuses: { value: Record<ServiceName, Status> }) { statuses.value = { ...statuses.value, [event.name]: event.status, }; } function statusFor(name: ServiceName, statuses: Record<ServiceName, Status>): Status { return statuses[name] ?? "unknown"; } <html class="page-status" ( services: ServiceName[], listener: Listener<StatusEvent>, private statuses: { value: Record<ServiceName, Status> } = { value: {} as Record<ServiceName, Status> }, ) oninit={() => listener.on("notify", (e) => onIncoming(e, statuses))}> <main class="page-shell is-form"> <h1>service status</h1> <ItemGroup label="Services" gap> <Item e:for={name of services} variant="outline"> <ItemContent> <ItemTitle>{name}</ItemTitle> </ItemContent> <ItemActions> <Badge variant={VARIANTS[statusFor(name, statuses.value)]} dot>{statusFor(name, statuses.value)}</Badge> </ItemActions> </Item> </ItemGroup> </main> </html>
statuses.value is a Record<ServiceName, Status>. onIncoming reassigns the
whole object ({ ...statuses.value, [name]: status }) so the binding re-runs
and the row's badge repaints. VARIANTS maps each status to a Badge variant
(success, warning, danger, secondary), which is where the dot color
lives, and dot draws it beside the status text so color is never the only
signal.
statusFor falls back to "unknown" for services without a broadcast yet.
That's the initial state on every fresh page load.
Each service is an Item in an ItemGroup, with the badge in ItemActions.
The scaffolded style.css keeps its one line, the import of
app/shared/styles/page.css; the page adds no rules of its own. Adding a new status ("maintenance") means three
changes: the Status union, its entry in VARIANTS, and any UI text that
lists the legend.
Routes
Register the page in index.ts:
index.tsimport status from "#app/pages/status"; // ... app.route("/status", status);
Notes
- Initial state is "unknown". On a fresh page load, no broadcasts have
arrived yet, so every service renders as gray. The first health-check tick
populates the real values. For a friendlier first paint, persist the latest
status per service in a
serviceStatusestable; the route reads it for initial render, the channel still drives live updates. - Triggers for
setStatus. Anything server-side can call it: anapp.cronhealth-check loop, a webhook from your monitoring vendor (aPOST /webhook/pagerdutyroute that parses the body and callssetStatus(...)), an admin button in another page. The recipe stays decoupled from the trigger. - Cron health check. Add an
app.cron("every 30s", "check services", () => new HealthCheckJob().schedule())and aHealthCheckJob.run()that pings each service (or hits an internal endpoint) and callssetStatus. The page picks up every change through the channel without any extra wiring. - Per-tenant status pages. Add
tenantIdtoStatusEventand filter the listener:statusChannel.listen({ filter: (e) => e.tenantId === currentTenantId }). The same broadcast channel handles every tenant; the filter splits the stream. - Why no LiveTable. LiveTable broadcasts on row insert/update/delete. Status events have no row identity to track (the latest value replaces the previous one, there is no history). The channel matches what the recipe needs: notify the new value, listen, replace local state.