Quickstart: a scene with a live reading
Create a Next.js app with a selectable 3D scene and a simulated occupancy reading, from a starter you download and run. You then have a starting point for connecting your own data.
Before you start
Package access. The @klorad packages are private: your npm account needs access before
npm install works (request package access;
Install explains it). Until then, the same twin runs in the
Live data example. Runtime. Node.js 18.18 or newer; the starter is
tested with Node.js 22 and npm 10. @klorad/protocols, which you add later for MQTT, webhooks
and REST, needs Node.js 20 or newer. Browser. One with WebGL for the 3D view.
Sign in to npm
npm login
npm view @klorad/api versionIf the second command prints a version, your account can install the packages. If it says the package is not found, see Troubleshooting.
Download the starter and run it
Download klorad-quickstart.zip, unzip it, and from the
klorad-quickstart folder:
npm install
npm run devOpen http://localhost:3100. You see an exhibition hall in Thessaloniki and its entrance counter in 3D, and above them a line such as "23 people at the entrance" that changes every second. Click an object to select it. The readings are simulated in your browser by a fixture source; nothing connects to a physical device.
Make a first change
In app/twin.tsx, move the counter to the other side of the hall: change its
position={{ east: 16, north: -10 }} to position={{ east: -16, north: 10 }} and save. The
page reloads and the counter stands in its new place, still counting. Positions are metres east
and north of the scene's origin, which is a latitude and longitude.
What is in the starter
The starter is a complete Next.js app, the same files the docs build in CI and run in their end
to end check: its
package.json, tsconfig.json, next.config.mjs, the page at / explained here, and the page
at /connected that the next guide explains.
"use client";
import { useMemo, useState } from "react";
import { fixtureSource } from "@klorad/api/integration";
import { Connector, KloradProvider, SceneObject, useLatest } from "@klorad/react";
import { ThreeView } from "@klorad/engine-three/react";
function Occupancy() {
const latest = useLatest("entrance", "occupancy");
return <p>{latest ? `${latest.value} ${latest.unit} at the entrance` : "Waiting for the first reading"}</p>;
}
export default function Twin() {
const [selected, setSelected] = useState<string | null>(null);
const source = useMemo(
() => fixtureSource({ id: "gateway", channels: [{ entity: "entrance-01", quantity: "occupancy", unit: "people", value: (t) => 20 + (t % 7) }] }),
[],
);
return (
<KloradProvider options={{ coordinateSystem: { origin: { lat: 40.62637, lon: 22.94838 } } }}>
<SceneObject id="hall" name="Exhibition hall" position={{ east: 0, north: 0 }} geometry={{ kind: "box", width: 24, depth: 14, height: 7 }} />
<SceneObject
id="entrance"
name="Entrance counter"
position={{ east: 16, north: -10 }}
geometry={{ kind: "cylinder", radius: 2, height: 4 }}
correspondence="shadow"
binding={{ source: "gateway", entity: "entrance-01" }}
/>
<Connector source={source} />
<Occupancy />
<div style={{ height: 480 }}>
<ThreeView selected={selected} onPick={setSelected} />
</div>
</KloradProvider>
);
}Three things to notice:
- Position is real. Every object is stored on Earth, from the origin you give the provider.
- The entrance is a Digital Shadow. Its
bindingnames a source and an entity, so readings fromgatewayaboutentrance-01land on it and nowhere else. The hall, which nothing physical updates, is a Digital Object. useLatestre-renders on change. It reads the current reading, ordered by when it was observed, and subscribes for you.
import Twin from "./twin";
export default function Page() {
return (
<main style={{ padding: 24 }}>
<h1>My first twin</h1>
<Twin />
</main>
);
}{
"name": "klorad-quickstart",
"private": true,
"engines": {
"node": ">=18.18"
},
"scripts": {
"dev": "next dev -p 3100",
"build": "next build",
"start": "next start -p 3100",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@klorad/api": "^0.2.0",
"@klorad/engine-three": "^0.2.0",
"@klorad/react": "^0.2.0",
"next": "15.5.28",
"react": "^19.2.1",
"react-dom": "^19.2.1",
"three": "^0.186.1",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/node": "^20.11.30",
"@types/react": "^19.0.0",
"@types/react-dom": "^19.0.0",
"@types/three": "^0.186.0",
"typescript": "^5.9.0"
}
}What you built
You now have a scene with a selected object and a changing reading. Next, replace the simulated source with data from your system.
Connect server data to your browser scene
The same counter, read on your server with its credential and shown in the browser: the starter's /connected page, step by step.
Style objects by readings
Map state to colour, height and labels with objectStyle.
How your system connects
MQTT, webhooks or a REST API: which guide fits what you run.
Something wrong or unclear? Tell us.