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 page shows the status from the most
recent broadcast for each service.
The recipe uses only a Channel. The page shows each service's current
status and keeps no history, so nothing is stored. All of the recipe's code is
in one page directory.
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 are declared in the page's template.ehtml. The route imports them
from there and passes 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. The code that runs the health checks (a cron job, a webhook handler,
an admin form) calls it 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 sets the dot color,
and dot draws the dot beside the status text so color is never the only
signal.
statusFor returns "unknown" for a service that has had no broadcast yet.
Every service is in that state on a 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 page code is the same whichever of them calls it. - 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 receives every change through the channel with no extra code. - Per-tenant status pages. Add
tenantIdtoStatusEventand filter the listener:statusChannel.listen({ filter: (e) => e.tenantId === currentTenantId }). Every tenant's events are sent on the same channel, and each listener receives only the events its filter accepts. - 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.