Skip to content

Follow fast data without slowing down ​

A camera, a microphone or a sensor can change data many times a second and subscribe runs at every change. Keep such a value out of subscribe and follow it with onData(name, listener). The listener receives each new value of that name alone and writes it straight into a canvas or a text node without touching the rest of the view. The sound meter in the example sends a new level twenty times a second.

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

// While you build, a fake sound meter sends a new level (0 to 1) every 50 ms.
const studio = {
	name: 'Studio',
	now: { text: 'Recording.' },
	data: { level: { every: 50, value: (time) => Math.abs(Math.sin(time / 400)) * 0.7 + Math.random() * 0.3 } },
};

const STEP = 3; // pixels per level on the canvas

// onData draws the level on a canvas and in a label, without React state.
function LevelMeter({ world }) {
	const canvas = useRef(null);
	const label = useRef(null);

	useEffect(() => {
		const context = canvas.current.getContext('2d');
		// onData calls this listener with each new level and for nothing else.
		return world.onData('level', (level) => {
			const { width, height } = canvas.current;
			// Move the picture one step left, then draw the new level at the right.
			context.drawImage(canvas.current, -STEP, 0);
			context.clearRect(width - STEP, 0, STEP, height);
			context.fillStyle = '#3d7ab8';
			context.fillRect(width - STEP, height - level * height, STEP, level * height);
			label.current.textContent = `${Math.round(level * 100)}%`;
		});
		// One call is enough, since `onData` works from any copy of `world`.
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, []);

	return (
		<>
			<canvas ref={canvas} width="300" height="80" style={{ border: '1px solid #8888' }} />
			{/* React never writes in this empty <span>, which leaves it free for onData. */}
			<p>Level: <span ref={label} /></p>
		</>
	);
}

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: studio } });

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;
	// This render also runs at every new level and only reads a name and a line of text.
	return (
		<>
			<h2>{world.name}</h2>
			<p>{world.now?.text}</p>
			<LevelMeter world={world} />
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// While you build, a fake sound meter sends a new level (0 to 1) every 50 ms.
const studio = {
	name: 'Studio',
	now: { text: 'Recording.' },
	data: { level: { every: 50, value: (time) => Math.abs(Math.sin(time / 400)) * 0.7 + Math.random() * 0.3 } },
};

const STEP = 3; // pixels per level on the canvas

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

const title = document.createElement('h2');
const now = document.createElement('p');
const canvas = document.createElement('canvas');
canvas.width = 300;
canvas.height = 80;
canvas.style.border = '1px solid #8888';
const levelLine = document.createElement('p');
const label = document.createElement('span');
levelLine.append('Level: ', label);
document.body.append(title, now, canvas, levelLine);

// subscribe, the slow part, also runs at every new level and only writes two texts.
world.subscribe((node) => {
	title.textContent = node.name;
	now.textContent = node.now?.text ?? '';
});

// onData, the fast part, calls this listener with each new level and for nothing else.
const context = canvas.getContext('2d');
world.onData('level', (level) => {
	const { width, height } = canvas;
	// Move the picture one step left, then draw the new level at the right.
	context.drawImage(canvas, -STEP, 0);
	context.clearRect(width - STEP, 0, STEP, height);
	context.fillStyle = '#3d7ab8';
	context.fillRect(width - STEP, height - level * height, STEP, level * height);
	label.textContent = `${Math.round(level * 100)}%`;
});
Live, on a fake world
Open

How it works ​

world.onData('level', listener) calls listener with each new level and for no other change. It returns a function that removes the listener. The React effect returns that function as its cleanup to stop the listener when the component unmounts. On each level the example shifts the canvas one step to the left (drawImage copies the canvas onto itself) and adds one bar at the right edge. This redraw touches a few pixels instead of the whole view.

  • In React the level never goes into state. The listener writes it on the canvas and in an empty <span> through refs that React never writes to. useWorld still re-renders the view at every change. That render, however, only reads a name and a line of text and leaves the DOM unchanged.
  • In plain JavaScript subscribe only writes two texts. When your subscribe renders more (a list, for example), render that part again only when it changed as Talk with the agent does.

However fast the data arrives, a change of data alone makes subscribe run at most once per animation frame. In the webapp onData follows the same pace. Draw from the latest value you receive and do not count on receiving every value.

See also ​

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