Skip to content

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;
	}));
});
Live, on a fake world
Open

How it works ​

  • member.status is 'online', 'connecting' or 'offline'. subscribe calls its listener again when a status changes and when a member joins or leaves.
  • member.role is the role of the member in the world and member.isMaster is true for its masters. The roles of the world are in world.roles and the role of the person in world.myRole.
  • Until the profile of a member arrives, its name can be null for 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 reads node.members ?? [] to handle that case.

See also ​

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