Skip to content

world.onData(name, listener) ​

Calls your listener with each new value of data[name] and returns a function that stops the calls.

onData(name: string, listener: (value: unknown) => void): () => void
js
const stop = world.onData('camera', (frame) => { picture.src = frame; });

Parameters ​

ParameterWhat it is
nameA name of data such as 'camera'.
listenerThe callback that receives each new value in the same form as in data.

Returns ​

A stop() function that ends the calls of your listener.

Usage ​

Follow one value ​

Because onData does not pass the current value, the view reads it from the world first. In this example the fake world sends a new temperature every half second and the view stops after five values:

jsx
import { useEffect, useState } from 'react';
import { useWorld } from './useWorld.js';

// A fake weather station that gives a new temperature every half second.
const station = {
	name: 'Weather station',
	data: { temperature: { every: 500, value: (time) => 20 + (Math.floor(time / 500) % 4) } },
};

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: station } });
	const [first, setFirst] = useState(null);
	const [values, setValues] = useState([]);
	const [isStopped, setIsStopped] = useState(false);
	const isConnected = world !== null;

	useEffect(() => {
		if (!isConnected) return undefined;
		// `onData` does not give the value the world has now. Read it from the world.
		setFirst(world.data.temperature);
		// Then it calls the function with each new value of one name of `data`.
		// It returns a function that stops it. This example stops after five values.
		let count = 0;
		const stop = world.onData('temperature', (value) => {
			count += 1;
			setValues((before) => [...before, value]);
			if (count === 5) {
				stop();
				setIsStopped(true);
			}
		});
		return stop;
		// Follow the value once, when the world arrives.
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [isConnected]);

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;
	return (
		<>
			<p>At the start: {first} °C</p>
			<ul>
				{values.map((value, index) => <li key={index}>New value: {value} °C</li>)}
			</ul>
			<p>{isStopped ? 'Stopped after 5 values.' : ''}</p>
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// A fake weather station that gives a new temperature every half second.
const station = {
	name: 'Weather station',
	data: { temperature: { every: 500, value: (time) => 20 + (Math.floor(time / 500) % 4) } },
};

// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: station } });

const first = document.createElement('p');
const list = document.createElement('ul');
const note = document.createElement('p');
document.body.append(first, list, note);

// `onData` does not give the value the world has now. Read it from the world.
first.textContent = `At the start: ${world.get().data.temperature} °C`;

// Then it calls the function with each new value of one name of `data`.
// It returns a function that stops it. This example stops after five values.
let count = 0;
const stop = world.onData('temperature', (value) => {
	count += 1;
	const item = document.createElement('li');
	item.textContent = `New value: ${value} °C`;
	list.append(item);
	if (count === 5) {
		stop();
		note.textContent = 'Stopped after 5 values.';
	}
});
Live, on a fake world
Open

Show a camera without drawing the whole view ​

A world that shows a camera changes many times a second. The listener writes each frame into one <img> and leaves the rest of your view untouched:

js
const picture = document.createElement('img');
picture.src = world.get().data.camera ?? '';
world.onData('camera', (frame) => {
  picture.src = frame;
});

In React ​

Once connectToHost has resolved, call onData in a useEffect that returns the stop function (the React tab above does exactly this).

Caveats ​

  • New values only. Your listener never receives the value the world has when you call onData. Read that value from world.get().data[name].
  • Inside the webapp the listener runs only when the value changes and at most about once per screen frame. A fake world calls it for every value it gives even when the value equals the previous one. Do not count calls to measure time.
  • Your subscribe listeners run too when a value changes. Use onData for one small job on one value and let the rest of the view render as usual.
  • The listener never runs for a name that never appears in data. Check the name in Object.keys(world.get().data).

Troubleshooting ​

onData never fails and has no error code.

ProblemWhyWhat to do
The image is empty at the start.onData passes only new values.Fill it from world.get().data first as in the camera example.
The listener is never called.The name is not in data or the value never changes.Look at Object.keys(world.get().data) or ask the owner of the world for the names.

See also ​

The UNaIVERSE GUI kit, released under the Apache License 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.