Docs
Concepts

Integration

Sources report readings about their own entities; connectSource routes them to the shadows and twins bound to them.

The Integration layer couples the world to reality. It depends on the World layer, which never imports it. Its central idea is that a source knows nothing about scenes: it reports readings about its own entities (entrance-01, vms-12, barrier-1), and the scene decides where they belong through each object's binding.

binding: { source: "gateway", entity: "entrance-01" }

That keeps vendor code and world code apart. You can swap a fixture for a real gateway, or one vendor for another, without touching a single Scene Object.

Sources

An ObservationSource has an id (matched against binding.source) and a start(sink) method that begins reporting and returns a function that stops it. These ship today:

SourcePackageWhat it doesUse it for
mqttConnection@klorad/protocols/mqttSubscribes to MQTT topics; publishes Twin commands.Gateways, PLCs and sensors on a broker.
webhookSource@klorad/protocolsReceives signed pushes over HTTP.Platforms that push events to you.
restSource@klorad/protocolsFetches a URL on an interval.Systems that only offer a pull API.
pollingSource@klorad/api/integrationCalls your read() on an interval.Anything else you can read, such as a vendor SDK.
fixtureSource@klorad/api/integrationGenerates readings from functions you give it.Demos, tests, the quickstart. Not a device.
fixtureDevice@klorad/api/integrationA simulated device that reports state and accepts commands.Demonstrating a Digital Twin's two way loop.

Prefer a source that pushes (MQTT, webhooks) when the system can push; polling is the fallback. Writing your own source is one object with two members.

Connecting

connectSource(scene, source) starts the source and routes every reading to every shadow or twin bound to that source and entity. It returns a Connection whose stats() counts what happened, which is the first thing to look at when data does not show up:

CounterMeaning
receivedReadings the source reported.
latest, late, duplicateWhat the scene did with them (see Time).
unboundNo object is bound to that source and entity. Usually a typo in a binding.
rejectedThe scene refused the reading, for example an invalid value.
connect-source.ts
import { createScene } from "@klorad/api/world";
import { connectSource, fixtureSource, pollingSource } from "@klorad/api/integration";

const scene = createScene({ coordinateSystem: { origin: { lat: 40.62637, lon: 22.94838 } } });
scene.add({
  id: "entrance",
  name: "Entrance counter",
  position: { east: 16, north: -10 },
  correspondence: "shadow",
  binding: { source: "gateway", entity: "entrance-01" },
});

// A fixture: generated readings once a second. A demo of the data path, not a device.
const fixture = fixtureSource({
  id: "gateway",
  channels: [{ entity: "entrance-01", quantity: "occupancy", unit: "people", value: (tick) => 20 + (tick % 7) }],
});
const connection = connectSource(scene, fixture);

// A real system that only offers a pull API, for example a connector's getStatus.
interface SignStatus {
  message: string;
  updatedAt: string;
}
declare const signs: { getStatus(ids: string[]): Promise<Record<string, SignStatus>> };

const vms = pollingSource({
  id: "atms",
  intervalMs: 15_000,
  read: async () => {
    const status = await signs.getStatus(["vms-12", "vms-14"]);
    return Object.entries(status).map(([entity, s]) => ({
      entity,
      quantity: "message",
      value: s.message,
      observedAt: Date.parse(s.updatedAt),
    }));
  },
});

export { connection, vms };

Outside data is not trusted

Validate every vendor payload at the boundary, inside read() or your push handler, before it becomes a reading. Connect a polling API shows the pattern with Zod. Credentials for vendor APIs belong on a server, never in browser code.

Devices and commands

mqttConnection is both a source and an actuator: give it a command mapping and an authorised Action on a Twin publishes its command to the device's topic (Connect MQTT devices).

A Digital Twin's commands leave through an Actuator: an object with a source (matched against the twin's binding.source) and a send(command) method that returns whether the device accepted it. fixtureDevice is both a source and an actuator, so it closes the loop the way a real device does: it accepts a command, and after a latency reports the new state as a reading. See Interaction and actuation.

Try it in your browserExample: Watch readings arrive, late and staleA reading that arrives late or twice never overwrites a newer one, and a quiet sensor shows as stale.

Something wrong or unclear? Tell us.