folkwaysDocs
Examples

Live dashboard

A backend publishes vehicle locations and alerts over HTTP; a public map shows them live with a publishable key.

A fleet backend knows where each vehicle is. A public status page shows them on a map and lists alerts. Nobody signs in to the page.

TopicEventPrimitiveWhy
fleet/{city}/locationlocation, one key per vehicleKV, 30 s TTLOnly the latest position matters; a vehicle that stops reporting drops off
fleet/{city}/alertsalertStreamEach alert matters, and a reload shows the latest ones

Rules

PatternTypesEventsOperations
fleet/*/locationKVlocation: KV, TTL 30,000 mssubscribe, publish, set
fleet/*/alertsStreamalert: Streamsubscribe, publish

Publish from the backend

Your backend publishes over HTTP, with no connection to keep open. Rest signs its own service token:

publisher.ts
TypeScript
import { Folkways } from '@folkwaysio/realtime/server';

const folkways = new Folkways.Rest({
  url: process.env.FOLKWAYS_URL!,
  segment: process.env.FOLKWAYS_SEGMENT_ID!,
  signingKey: { kid: process.env.FOLKWAYS_KEY_ID!, privateKey: process.env.FOLKWAYS_PRIVATE_KEY! },
  identity: 'fleet-backend',
  capabilities: [{ topics: 'fleet/>', ops: ['publish'] }],
});

export async function reportPosition(city: string, vehicleId: string, lat: number, lng: number) {
  await folkways.publish(`fleet/${city}/location`, 'location', { lat, lng, at: Date.now() }, {
    delivery: 'kv',
    key: vehicleId,
    ttlMs: 30_000,
  });
}

export async function raiseAlert(city: string, alertId: string, text: string) {
  // A stable idempotency key makes a retried alert safe.
  await folkways.publish(`fleet/${city}/alerts`, 'alert', { text }, { idempotencyKey: alertId });
}

Call reportPosition every few seconds per vehicle; well inside the 30-second TTL. alertId must be a UUID.

Show it on a public page

The page uses a publishable key that may read fleet/>. Turn on Allow anonymous connections in the segment's settings first.

dashboard.ts
TypeScript
import { Folkways } from '@folkwaysio/realtime';

declare function placeMarkers(vehicles: { id: string; lat: number; lng: number }[]): void;
declare function showAlerts(alerts: string[]): void;

const client = new Folkways.Realtime({
  url: 'https://realtime.folkways.net',
  publishableKey: 'YOUR_PUBLISHABLE_KEY', // from the segment's Publishable keys tab
});
await client.connect();

const locations = client.topic('fleet/tallinn/location');
const alerts = client.topic('fleet/tallinn/alerts');

const map = locations.values('location').open();
const feed = alerts.messages.open();

map.subscribe(() => {
  placeMarkers([...map.getSnapshot()].map(([id, m]) => ({ id, ...(m.data as { lat: number; lng: number }) })));
});
feed.subscribe(() => {
  showAlerts(feed.getSnapshot().slice(-20).map((m) => (m.data as { text: string }).text));
});

await Promise.all([
  locations.subscribe().ready,                     // a snapshot of every live vehicle, then changes
  alerts.subscribe({ from: { last: 20 } }).ready,  // the last 20 alerts, then new ones
]);

How it behaves

  • A new viewer sees every vehicle at once, from the KV snapshot, and the last 20 alerts from the stream.
  • A vehicle that stops reporting disappears 30 seconds later: its value expires and viewers get a deletion.
  • Behind a firewall that blocks WebSockets, the page falls back to long-polling and still updates.
  • Viewers can't publish: publishable keys only issue read-only tokens.

Many viewers? Each delivery to each viewer counts as a message. A map with 50 vehicles reporting every 5 seconds and 1,000 viewers is about 10,000 deliveries a second: check it against your package, and choose the Live dashboard rate preset for the segment. Report less often, or only when a vehicle moved, to cut usage.

On this page