Show the agent and its camera
The first version of the view connects to Feeder and renders its name, its description and the frames of its camera as they arrive.
A fake agent
While you build, Feeder is a fake agent that you define in your own file and that connectToHost runs in your page:
const feeder = {
name: 'Feeder',
profile: { description: 'A camera at a bird feeder, in a garden.' },
data: { camera: { every: 100, value: frameAt } },
};
const agent = await connectToHost({ fake: { agent: feeder } });fake: { agent }creates an agent instead of a world (fake: { world }). A view reads both through the same API.datamaps each name the agent shows to its value. Feeder shows onlycamera, whose value the fake agent computes by callingframeAt(time)every 100 ms (ten times a second).frameAtdraws a robin at a feeder as an SVG and returns it as adata:URI. The view handles the frames of a real agent in the same way because they also arrive indataas strings ready forimg.src.
Inside the webapp you call connectToHost() without fake and its Promise resolves to the agent that the person opened in the webapp.
Draw the agent
agent.subscribe(listener) calls listener at once and then after every change of the agent (a new camera frame counts as a change).
agent.subscribe((node) => {
title.textContent = node.name;
description.textContent = node.profile.description ?? '';
const frame = node.data.camera;
if (frame) picture.src = frame;
});node.profile.descriptionis the description in the profile of the agent and isnullwhen there is none.node.data.camerais a URL or data URI ready for thesrcof an<img>. It is missing until the first frame arrives and in the meantime the caption reads "Waiting for the first picture…".node.statusis'connecting'while the agent connects and then becomes'online'. While the status is'connecting', the caption reads "Connecting to Feeder…".
In React the useWorld hook subscribes for you and re-renders the component with the newest agent on every change. The example names the variable agent instead of world:
const { world: agent, error } = useWorld({ fake: { agent: feeder } });The code
import { useWorld } from './useWorld.js';
// The camera of the fake agent draws a robin at a bird feeder as an SVG picture.
// Every picture is a little different, because `time` moves the bird.
function frameAt(time) {
const x = 170 + 70 * Math.sin(time / 800);
const y = 120 - Math.abs(25 * Math.sin(time / 300));
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="320" height="180">
<rect width="320" height="180" fill="#bfe3ff"/><rect y="140" width="320" height="40" fill="#7cc576"/>
<rect x="40" y="70" width="10" height="70" fill="#8b5a2b"/><rect x="18" y="66" width="54" height="8" fill="#8b5a2b"/><polygon points="12,50 78,50 45,30" fill="#a0522d"/>
<circle cx="${x}" cy="${y}" r="14" fill="#d35400"/><circle cx="${x + 12}" cy="${y - 12}" r="8" fill="#6e4b2a"/>
<polygon points="${x + 19},${y - 13} ${x + 28},${y - 10} ${x + 19},${y - 8}" fill="#f1c40f"/>
<text x="310" y="20" font-size="13" font-family="monospace" text-anchor="end">${new Date(time).toLocaleTimeString('en-GB')}</text>
</svg>`;
return `data:image/svg+xml,${encodeURIComponent(svg)}`;
}
// A fake agent to build with. Its camera sends a new picture every 100 ms.
const feeder = {
name: 'Feeder',
profile: { description: 'A camera at a bird feeder, in a garden.' },
data: { camera: { every: 100, value: frameAt } },
};
export default function App() {
// While building: the fake agent. In the webapp: useWorld()
const { world: agent, error } = useWorld({ fake: { agent: feeder } });
if (error) return <p>Could not connect ({error.code}).</p>;
if (!agent) return <p>Connecting…</p>;
// `data.camera` is an image source, ready for an <img>. It is missing until the first picture arrives.
const frame = agent.data.camera;
let caption = frame ? `Live from ${agent.name}'s camera` : 'Waiting for the first picture…';
if (agent.status === 'connecting') caption = `Connecting to ${agent.name}…`;
return (
<>
<h1>{agent.name}</h1>
<p>{agent.profile.description}</p>
{frame && <img src={frame} alt={`${agent.name}'s camera`} width="320" height="180" />}
<p>{caption}</p>
</>
);
}import { connectToHost } from '@unaiverse/gui-kit';
// The camera of the fake agent draws a robin at a bird feeder as an SVG picture.
// Every picture is a little different, because `time` moves the bird.
function frameAt(time) {
const x = 170 + 70 * Math.sin(time / 800);
const y = 120 - Math.abs(25 * Math.sin(time / 300));
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="320" height="180">
<rect width="320" height="180" fill="#bfe3ff"/><rect y="140" width="320" height="40" fill="#7cc576"/>
<rect x="40" y="70" width="10" height="70" fill="#8b5a2b"/><rect x="18" y="66" width="54" height="8" fill="#8b5a2b"/><polygon points="12,50 78,50 45,30" fill="#a0522d"/>
<circle cx="${x}" cy="${y}" r="14" fill="#d35400"/><circle cx="${x + 12}" cy="${y - 12}" r="8" fill="#6e4b2a"/>
<polygon points="${x + 19},${y - 13} ${x + 28},${y - 10} ${x + 19},${y - 8}" fill="#f1c40f"/>
<text x="310" y="20" font-size="13" font-family="monospace" text-anchor="end">${new Date(time).toLocaleTimeString('en-GB')}</text>
</svg>`;
return `data:image/svg+xml,${encodeURIComponent(svg)}`;
}
// A fake agent to build with. Its camera sends a new picture every 100 ms.
const feeder = {
name: 'Feeder',
profile: { description: 'A camera at a bird feeder, in a garden.' },
data: { camera: { every: 100, value: frameAt } },
};
// While building: the fake agent. In the webapp: const agent = await connectToHost();
const agent = await connectToHost({ fake: { agent: feeder } });
// The page shows the name of the agent, what it is and its camera.
const title = document.createElement('h1');
const description = document.createElement('p');
const picture = document.createElement('img');
picture.width = 320;
picture.height = 180;
const caption = document.createElement('p');
document.body.append(title, description, picture, caption);
// Called at once, then after every change of the agent, a new picture included.
agent.subscribe((node) => {
title.textContent = node.name;
description.textContent = node.profile.description ?? '';
// `data.camera` is an image source, ready for an <img>. It is missing until the first picture arrives.
const frame = node.data.camera;
picture.hidden = !frame;
if (frame) picture.src = frame;
if (node.status === 'connecting') caption.textContent = `Connecting to ${node.name}…`;
else caption.textContent = frame ? `Live from ${node.name}'s camera` : 'Waiting for the first picture…';
});The bird moves and the clock in the corner advances because every frame is a new picture.
Next steps
Each of the ten frames a second still re-renders the whole view. In step 2: Follow the pictures of the camera the camera gets a listener of its own.
The data of the world covers data in full and subscribe has its own page in the reference.