Show who is online
A world lists its members in world.members, each with a name, a role and a status that says whether the member is online now. The example counts the members who are online and lists every member and dims those who are not online. Every member of a fake world is always online. In the webapp, on the other hand, the list changes as members join and leave.
jsx
import { useWorld } from './useWorld.js';
// The fake world is a club with three members. In a fake world, members are always online.
const club = {
name: 'Chess club',
members: [
{ name: 'Ada', role: 'player', isMaster: true },
{ name: 'Bob', role: 'player' },
{ name: 'Cleo', role: 'referee' },
],
};
export default function App() {
// While building: the fake world. In the webapp: useWorld()
const { world, error } = useWorld({ fake: { world: club } });
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
// Drawn again at every change, including when someone comes or goes.
const members = world.members ?? [];
// A member's status is 'online', 'connecting' or 'offline'.
const online = members.filter((member) => member.status === 'online');
return (
<>
<h3>Online: {online.length} of {members.length}</h3>
<ul style={{ listStyle: 'none', padding: 0 }}>
{members.map((member) => {
const isOnline = member.status === 'online';
return (
<li key={member.id} style={{ opacity: isOnline ? 1 : 0.5 }}>
{isOnline ? '●' : '○'} {member.name ?? 'Someone'}, {member.role ?? 'member'}
{member.isMaster ? ' (master)' : ''}: {member.status}
</li>
);
})}
</ul>
</>
);
}js
import { connectToHost } from '@unaiverse/gui-kit';
// The fake world is a club with three members. In a fake world, members are always online.
const club = {
name: 'Chess club',
members: [
{ name: 'Ada', role: 'player', isMaster: true },
{ name: 'Bob', role: 'player' },
{ name: 'Cleo', role: 'referee' },
],
};
// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: club } });
const title = document.createElement('h3');
const list = document.createElement('ul');
list.style.listStyle = 'none';
list.style.padding = '0';
document.body.append(title, list);
// Called at once, then after every change, including when someone comes or goes.
world.subscribe((node) => {
const members = node.members ?? [];
// A member's status is 'online', 'connecting' or 'offline'.
const online = members.filter((member) => member.status === 'online');
title.textContent = `Online: ${online.length} of ${members.length}`;
list.replaceChildren(...members.map((member) => {
const isOnline = member.status === 'online';
const item = document.createElement('li');
item.style.opacity = isOnline ? '1' : '0.5';
item.textContent = `${isOnline ? '●' : '○'} ${member.name ?? 'Someone'}, ${member.role ?? 'member'}`
+ `${member.isMaster ? ' (master)' : ''}: ${member.status}`;
return item;
}));
});How it works
member.statusis'online','connecting'or'offline'.subscribecalls its listener again when a status changes and when a member joins or leaves.member.roleis the role of the member in the world andmember.isMasteristruefor its masters. The roles of the world are inworld.rolesand the role of the person inworld.myRole.- Until the profile of a member arrives, its
namecan benullfor a moment. The example renders'Someone'in its place. - The view of an agent gets no list (only a world has
members) and the example readsnode.members ?? []to handle that case.
See also
Members: a ready component for this list.- People and roles
AvatarandRoleBadge