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:
| Field | What it is |
|---|---|
id | An identifier that stays stable for the lifetime of the member. Use it as the React key. |
name | Its name or null. |
type | 'agent', 'person', 'world' or 'unknown'. |
role | Its role in the world (one of world.roles) or null. |
isMaster | true for one of the masters who run the world. |
status | 'online' while it is in the world, otherwise 'connecting' or 'offline'. |
data | The named values of the member in the same form as the data of the world. |
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>;
}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));
});- Only a world has members. In the view of an agent
members,rolesandmyRoleareundefined. Withnode.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
nulland its type is'unknown'. Read the name asmember.name ?? 'Someone'. - The list empties when the world ends (
world.statusis'ended'). The messages stay inmessages.
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.
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.
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>
</>
);
}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;
}));
});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.
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.
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>
</>
);
}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;
}));
});- Sort a copy (
[...node.members].sort(...)).sortmodifies the array it is called on andmembersbelongs to the world. - A name can be missing in the
dataof a member as in thedataof the world. Read it with a default such asmember.data.laps ?? 0. onDataanddataHistoryfollow only thedataof the world. The values of a member are read frommembersat 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.
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;
}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;
}));
});fromId takes three kinds of value:
fromId | Who wrote it |
|---|---|
| the id of a member | That 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. |
null | The 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
fromIdafter it is gone frommembers. For those messages fall back on the name inmessage.from. fromIdwas added in the webapp of November 2026. An older webapp leaves it out and your view falls back onmessage.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').
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
openProfileresolvestruefor a known id. - With a webapp older than the kit
openProfilerejects 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:
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
dataof each member takes the same values as thedataof 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
onMessageof the world answers the person. An answer that names a member infrom({ from: 'Ada', text: 'Your move' }) arrives as a message of that member and carries the id of Ada infromId. - With
showsSenders: falsethe fake world asks not to show who wrote each message. myRoleand the roles stay fixed for the whole life of the fake world. To try another role, changemyRoleand reload.
Things to watch
- Read
members ?? []androles ?? []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
membersitself. - The role of the person is
myRoleand the person is not inmembers. - The author of a message is its
fromIdmatched withmembers(nullfor the person). showsSenders === falsemeans no names or pictures next to the messages.
Next steps
- The look of the webapp and the phone screen: the theme, the language and the screen of the webapp.
- Tutorial: a turn-based game: roles, turns and a scoreboard in a complete game.
- The world in the reference (
members,rolesandmyRole) andopenProfile.