Skip to content

People and roles ​

Besides the person who uses your view, a world contains agents, other people and the masters who run it. Each of them has a role (like the person) and data of its own. With these fields your view lists who is in the world and who is online, shows the roles and the role of the person, builds a scoreboard from the data of each member and shows the author of each message with a button that opens its profile card.

The members ​

world.members lists the other members of the world. Each member has these fields:

FieldWhat it is
idAn identifier that stays stable for the lifetime of the member. Use it as the React key.
nameIts name or null.
type'agent', 'person', 'world' or 'unknown'.
roleIts role in the world (one of world.roles) or null.
isMastertrue for one of the masters who run the world.
status'online' while it is in the world, otherwise 'connecting' or 'offline'.
dataThe named values of the member in the same form as the data of the world.
jsx
import { useWorld } from './useWorld.js';

// A fake world with three members: two players and a referee, who is a master of the world.
const arena = {
	name: 'Arena',
	roles: [{ name: 'player', description: 'Plays the game' }, { name: 'referee', description: 'Keeps the score' }],
	members: [
		{ name: 'Ada', role: 'player' },
		{ name: 'Bo', role: 'player' },
		{ name: 'Rita', role: 'referee', isMaster: true },
	],
};

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

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;

	// Only a world has members, and the view of an agent gets none.
	const members = world.members ?? [];
	const online = members.filter((member) => member.status === 'online');
	return (
		<>
			<h2>{members.length} in the world, {online.length} online</h2>
			<ul>
				{members.map((member) => <MemberLine key={member.id} member={member} />)}
			</ul>
		</>
	);
}

// One line per member: name, kind, role, master or not, online or not.
function MemberLine({ member }) {
	const parts = [member.name ?? 'Someone', member.type, member.role ?? 'no role'];
	if (member.isMaster) parts.push('master');
	parts.push(member.status === 'online' ? 'online' : 'away');
	return <li>{parts.join(' · ')}</li>;
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// A fake world with three members: two players and a referee, who is a master of the world.
const arena = {
	name: 'Arena',
	roles: [{ name: 'player', description: 'Plays the game' }, { name: 'referee', description: 'Keeps the score' }],
	members: [
		{ name: 'Ada', role: 'player' },
		{ name: 'Bo', role: 'player' },
		{ name: 'Rita', role: 'referee', isMaster: true },
	],
};

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

const count = document.createElement('h2');
const list = document.createElement('ul');
document.body.append(count, list);

// One line per member: name, kind, role, master or not, online or not.
function memberLine(member) {
	const parts = [member.name ?? 'Someone', member.type, member.role ?? 'no role'];
	if (member.isMaster) parts.push('master');
	parts.push(member.status === 'online' ? 'online' : 'away');
	const item = document.createElement('li');
	item.textContent = parts.join(' · ');
	return item;
}

// Called at once, then after every change of the world.
world.subscribe((node) => {
	// Only a world has members, and the view of an agent gets none.
	const members = node.members ?? [];
	const online = members.filter((member) => member.status === 'online');
	count.textContent = `${members.length} in the world, ${online.length} online`;
	list.replaceChildren(...members.map(memberLine));
});
Live, on a fake world
Open
  • Only a world has members. In the view of an agent members, roles and myRole are undefined. With node.members ?? [] the same code works in both views.
  • The person is not in the list. The role of the person is world.myRole (below).
  • A member may not have introduced itself yet. Until it does, its name or its role is null and its type is 'unknown'. Read the name as member.name ?? 'Someone'.
  • The list empties when the world ends (world.status is 'ended'). The messages stay in messages.

Who is online ​

member.status is 'online' when the member is in the world. 'connecting' (while it reconnects) and 'offline' mean that it is not reachable now. Compare with 'online' and treat the other two values as away.

js
const members = node.members ?? [];
const online = members.filter((member) => member.status === 'online');
count.textContent = `${online.length} of ${members.length} online`;

Every arrival or departure of a member is a change of the world that calls your subscribe listener with the new list. The recipe Show who is online and the component Members build on this code.

Roles ​

world.roles lists the roles of the world. Each role has a name, a description (or null) and a diagram that holds the rules of the role as the world describes them (most views never read it). world.myRole is the name of the role of the person and is null while the person has none.

jsx
import { useWorld } from './useWorld.js';

// A fake world with two roles. The person is a player.
const arena = {
	name: 'Arena',
	roles: [{ name: 'player', description: 'Plays the game' }, { name: 'referee', description: 'Keeps the score' }],
	members: [
		{ name: 'Ada', role: 'player' },
		{ name: 'Bo', role: 'player' },
		{ name: 'Rita', role: 'referee', isMaster: true },
	],
	myRole: 'player',
};

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

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;

	const roles = world.roles ?? [];
	const members = world.members ?? [];
	// The person's role, with its description when the world gives one.
	const myRole = roles.find((role) => role.name === world.myRole);
	return (
		<>
			<h2>
				{world.myRole ? `Your role: ${world.myRole} (${myRole?.description ?? 'no description'})` : 'You have no role yet.'}
			</h2>
			{/* Each role, with how many members have it. */}
			<ul>
				{roles.map((role) => (
					<li key={role.name} style={{ fontWeight: role.name === world.myRole ? 'bold' : 'normal' }}>
						{role.name}: {role.description ?? ''} ({members.filter((member) => member.role === role.name).length})
					</li>
				))}
			</ul>
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// A fake world with two roles. The person is a player.
const arena = {
	name: 'Arena',
	roles: [{ name: 'player', description: 'Plays the game' }, { name: 'referee', description: 'Keeps the score' }],
	members: [
		{ name: 'Ada', role: 'player' },
		{ name: 'Bo', role: 'player' },
		{ name: 'Rita', role: 'referee', isMaster: true },
	],
	myRole: 'player',
};

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

const mine = document.createElement('h2');
const list = document.createElement('ul');
document.body.append(mine, list);

// Called at once, then after every change of the world.
world.subscribe((node) => {
	const roles = node.roles ?? [];
	const members = node.members ?? [];
	// The person's role, with its description when the world gives one.
	const myRole = roles.find((role) => role.name === node.myRole);
	mine.textContent = node.myRole ? `Your role: ${node.myRole} (${myRole?.description ?? 'no description'})` : 'You have no role yet.';
	// Each role, with how many members have it.
	list.replaceChildren(...roles.map((role) => {
		const holders = members.filter((member) => member.role === role.name).length;
		const item = document.createElement('li');
		item.textContent = `${role.name}: ${role.description ?? ''} (${holders})`;
		if (role.name === node.myRole) item.style.fontWeight = 'bold';
		return item;
	}));
});
Live, on a fake world
Open

The role of the person often decides what your view shows (a referee sees the controls of the match and a player sees the board). Since a role can change during the life of the world (a new round, a promotion), read myRole at every change like any other field.

js
world.subscribe((node) => {
  refereePanel.hidden = node.myRole !== 'referee';
  board.hidden = node.myRole !== 'player';
});

The component Avatar and RoleBadge shows a member with its role.

A member's data ​

The data of a member holds values of the same kinds as the data of the world (numbers, text, grids, pictures, files) such as an avatar, the score of a player or the position of a robot. The scoreboard below reads it in the subscribe listener together with the rest of the world and sorts the members by laps.

jsx
import { useWorld } from './useWorld.js';

// A fake race: each runner shows its own laps in its `data`.
const race = {
	name: 'Race',
	members: [
		{ name: 'Ada', role: 'runner', data: { laps: (time) => Math.floor(time / 1000) % 10 } },
		{ name: 'Bo', role: 'runner', data: { laps: (time) => Math.floor(time / 1500) % 10 } },
		{ name: 'Cy', role: 'runner', data: { laps: 4 } },
	],
};

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

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;

	// Sort a copy, because `members` belongs to the world and the view never changes it.
	const ranking = [...(world.members ?? [])].sort((a, b) => (b.data.laps ?? 0) - (a.data.laps ?? 0));
	return (
		<>
			<h2>Scoreboard</h2>
			<ol>
				{ranking.map((member) => (
					<li key={member.id}>{member.name}: {member.data.laps ?? 0} laps</li>
				))}
			</ol>
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// A fake race: each runner shows its own laps in its `data`.
const race = {
	name: 'Race',
	members: [
		{ name: 'Ada', role: 'runner', data: { laps: (time) => Math.floor(time / 1000) % 10 } },
		{ name: 'Bo', role: 'runner', data: { laps: (time) => Math.floor(time / 1500) % 10 } },
		{ name: 'Cy', role: 'runner', data: { laps: 4 } },
	],
};

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

const title = document.createElement('h2');
title.textContent = 'Scoreboard';
const list = document.createElement('ol');
document.body.append(title, list);

// Called at once, then after every change of the world.
world.subscribe((node) => {
	// Sort a copy, because `members` belongs to the world and the view never changes it.
	const ranking = [...(node.members ?? [])].sort((a, b) => (b.data.laps ?? 0) - (a.data.laps ?? 0));
	list.replaceChildren(...ranking.map((member) => {
		const item = document.createElement('li');
		item.textContent = `${member.name}: ${member.data.laps ?? 0} laps`;
		return item;
	}));
});
Live, on a fake world
Open
  • Sort a copy ([...node.members].sort(...)). sort modifies the array it is called on and members belongs to the world.
  • A name can be missing in the data of a member as in the data of the world. Read it with a default such as member.data.laps ?? 0.
  • onData and dataHistory follow only the data of the world. The values of a member are read from members at every change.

The component Scoreboard is a ready scoreboard to copy.

Who wrote it ​

The fromId of a message is the id of its author in members. The member with that id gives you the name, the role and the data of the author.

jsx
import { useWorld } from './useWorld.js';

// A fake world with two members. `from` makes one of them say the answer.
const arena = {
	name: 'Arena',
	members: [{ name: 'Ada', role: 'player' }, { name: 'Rita', role: 'referee', isMaster: true }],
	onMessage: () => ({ from: 'Rita', text: 'Ada, your move' }),
};

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

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;
	return (
		<>
			<button onClick={() => world.send('hello')}>Say hello</button>
			<ul>
				{world.messages.map((message) => (
					<li key={message.id}>
						{/* `showsSenders` is false when the world asks not to show who wrote each message. */}
						{world.showsSenders === false ? '' : `${authorOf(message, world)}: `}
						{message.text ?? ''}
					</li>
				))}
			</ul>
		</>
	);
}

// Who wrote a message, in words: the person, the world itself, or a member with its role.
function authorOf(message, world) {
	if (message.fromMe) return 'You';
	if (message.fromId === world.id) return world.name;
	// `fromId` is a member's id, as in `members`.
	const member = (world.members ?? []).find((someone) => someone.id === message.fromId);
	return member ? `${member.name} (${member.role})` : message.from;
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// A fake world with two members. `from` makes one of them say the answer.
const arena = {
	name: 'Arena',
	members: [{ name: 'Ada', role: 'player' }, { name: 'Rita', role: 'referee', isMaster: true }],
	onMessage: () => ({ from: 'Rita', text: 'Ada, your move' }),
};

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

const button = document.createElement('button');
button.textContent = 'Say hello';
button.onclick = () => world.send('hello');
const list = document.createElement('ul');
document.body.append(button, list);

// Who wrote a message, in words: the person, the world itself, or a member with its role.
function authorOf(message, node) {
	if (message.fromMe) return 'You';
	if (message.fromId === node.id) return node.name;
	// `fromId` is a member's id, as in `members`.
	const member = (node.members ?? []).find((someone) => someone.id === message.fromId);
	return member ? `${member.name} (${member.role})` : message.from;
}

// Called at once, then after every change of the world.
world.subscribe((node) => {
	list.replaceChildren(...node.messages.map((message) => {
		const item = document.createElement('li');
		// `showsSenders` is false when the world asks not to show who wrote each message.
		item.textContent = node.showsSenders === false
			? message.text ?? ''
			: `${authorOf(message, node)}: ${message.text ?? ''}`;
		return item;
	}));
});
Live, on a fake world
Open

fromId takes three kinds of value:

fromIdWho wrote it
the id of a memberThat member, listed in members.
the id of the world (world.id)The world itself for its notes and its own messages. In the view of an agent this is the id of the agent.
nullThe person (fromMe is true). Your view never gets the id of the person and finds the nickname in me.
  • A member can leave. Its old messages keep its fromId after it is gone from members. For those messages fall back on the name in message.from.
  • fromId was added in the webapp of November 2026. An older webapp leaves it out and your view falls back on message.from.

When the world hides who wrote it ​

world.showsSenders is false when a world asks not to show who wrote each message. Like the example, show the messages without names and without pictures of their authors. A webapp older than November 2026 leaves showsSenders out. With the test node.showsSenders === false your view then keeps showing the names.

showsSenders states a wish of the owner and filters nothing. The messages still carry from and fromId and members still lists the members. Your view can still show the list of the members without going against the wish because the webapp shows the same list in its own view.

Open a member's card ​

world.openProfile(id) asks the webapp to open its own profile card of a member over your view. It takes the id of a member or world.id for the card of the world itself. The promise resolves true when the card is shown and false for any other id or for a member that has not introduced itself yet (type 'unknown').

js
function authorButton(message, node) {
  const author = (node.members ?? []).find((member) => member.id === message.fromId);
  const button = document.createElement('button');
  button.textContent = author?.name ?? message.from;
  button.disabled = !message.fromId; // the person's own messages: no card to open
  button.onclick = () => world.openProfile(message.fromId);
  return button;
}
  • Open a card only on a click of the person. A card that the person did not ask for covers the view.
  • Your view gets nothing back from the card that the person reads and closes in the webapp.
  • With a fake world no card appears and openProfile resolves true for a known id.
  • With a webapp older than the kit openProfile rejects with 'not-allowed-verb'. Hide the buttons that open cards when it does.

world.openProfile(id) has every detail.

People in a fake world ​

A fake world takes members, roles and myRole:

js
const arena = {
  name: 'Arena',
  roles: [{ name: 'player', description: 'Plays the game' }, { name: 'referee' }],
  members: [
    { name: 'Ada', role: 'player', data: { score: (time) => Math.floor(time / 1000) % 10 } },
    { name: 'Rita', role: 'referee', isMaster: true },
  ],
  myRole: 'player',
};
  • The data of each member takes the same values as the data of the world (fixed values, functions of time or { every, value } as in Data in a fake world).
  • Fake members are agents that are always online. The onMessage of the world answers the person. An answer that names a member in from ({ from: 'Ada', text: 'Your move' }) arrives as a message of that member and carries the id of Ada in fromId.
  • With showsSenders: false the fake world asks not to show who wrote each message.
  • myRole and the roles stay fixed for the whole life of the fake world. To try another role, change myRole and reload.

Things to watch ​

  • Read members ?? [] and roles ?? [] because the view of an agent has neither.
  • Read name ?? 'Someone' for a member that has not introduced itself yet.
  • Compare status === 'online' and treat 'connecting' and 'offline' as away.
  • Sort a copy and never members itself.
  • The role of the person is myRole and the person is not in members.
  • The author of a message is its fromId matched with members (null for the person).
  • showsSenders === false means no names or pictures next to the messages.

Next steps ​

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