Skip to content

Show who wrote each message ​

The members of a world, people and agents alike, talk in the same conversation and each of them has a role. message.from is the name of the sender and names are not unique (two members can both be called Ada). message.fromId, on the other hand, is the id of the sender as it appears in world.members and points to the one member who wrote the message. Ask the museum staff in the example twice to see a different member answer each time.

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

// In this fake museum, two members answer in turn.
// `from` in an answer names the member who says it.
let turn = 0;
const museum = {
	name: 'Museum',
	members: [
		{ name: 'Ada', role: 'guide' },
		{ name: 'Bob', role: 'guard' },
	],
	onMessage: () => {
		turn += 1;
		if (turn % 2 === 1) return { from: 'Ada', text: 'Of course! The paintings are on the first floor.' };
		return { from: 'Bob', text: 'Sure. The exit is on your left.' };
	},
};

// Who wrote a message, as "You" or as the member who wrote it, with their role.
function authorOf(message, world) {
	if (message.fromMe) return 'You';
	// `fromId` is the member's id, as in `members`. An older webapp gives none, and only the name shows.
	const member = (world.members ?? []).find((item) => item.id === message.fromId);
	if (!member) return message.from ?? 'Someone';
	return `${member.name ?? 'Someone'} (${member.role ?? 'no role'})`;
}

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

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;
	return (
		<>
			{world.messages.map((message) => (
				<p key={message.id}><b>{authorOf(message, world)}</b>: {message.text}</p>
			))}
			<button onClick={() => world.send('Can you help me?')} disabled={!world.canSend}>Ask the staff</button>
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// In this fake museum, two members answer in turn.
// `from` in an answer names the member who says it.
let turn = 0;
const museum = {
	name: 'Museum',
	members: [
		{ name: 'Ada', role: 'guide' },
		{ name: 'Bob', role: 'guard' },
	],
	onMessage: () => {
		turn += 1;
		if (turn % 2 === 1) return { from: 'Ada', text: 'Of course! The paintings are on the first floor.' };
		return { from: 'Bob', text: 'Sure. The exit is on your left.' };
	},
};

// Who wrote a message, as "You" or as the member who wrote it, with their role.
function authorOf(message, node) {
	if (message.fromMe) return 'You';
	// `fromId` is the member's id, as in `members`. An older webapp gives none, and only the name shows.
	const member = (node.members ?? []).find((item) => item.id === message.fromId);
	if (!member) return message.from ?? 'Someone';
	return `${member.name ?? 'Someone'} (${member.role ?? 'no role'})`;
}

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

const list = document.createElement('div');
const ask = document.createElement('button');
ask.textContent = 'Ask the staff';
ask.onclick = () => world.send('Can you help me?');
document.body.append(list, ask);

world.subscribe((node) => {
	ask.disabled = !node.canSend;
	list.replaceChildren(...node.messages.map((message) => {
		const line = document.createElement('p');
		const author = document.createElement('b');
		author.textContent = authorOf(message, node);
		line.append(author, `: ${message.text ?? ''}`);
		return line;
	}));
});
Live, on a fake world
Open

How it works ​

fromId holds the id of the member for a message of a member, the id of the world for its notes and null for the messages of the person. members.find((member) => member.id === message.fromId) returns the member with their name, role, type and status. When no member has that id (the case of the notes of the world), authorOf shows the name in from.

  • When two members share a name, a lookup by from gives both Adas the role of the first one while a lookup by fromId gives each member their own role. (The two members of the example have different names because the from of a fake answer picks a member by name.)
  • The messages of the person have fromId: null on purpose. Your view knows the person only through world.me and only through what they chose to share when they accepted the view.
  • With an older webapp that has no fromId, the view shows from for everyone.
  • When the world asks to hide who wrote each message, show none of this (Hide senders when the world asks).

See also ​

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