Skip to content

Explain a refused message ​

world.send resolves with 'not-allowed' and sends nothing in three cases: (1) the value is empty, (2) the world cannot be reached and (3) the rules of the world do not accept a message now. A turn-based game, for example, refuses messages outside the turn of the person. Tell the person which case applies and keep the text they wrote in the box. The live example can show only the refusal of an empty box (press Send with nothing written) because a fake world always accepts messages.

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

// While you build, a fake game answers every move.
const arena = {
	name: 'Arena',
	now: { text: 'Your move, white' },
	onMessage: (message) => `Move taken: ${message.text}`,
};

// Says why the world did not take a message, in the person's words.
function whyNotAllowed(node, text) {
	if (!text.trim()) return 'Write your move first.';
	if (!node.isOnline) return 'The world cannot be reached now. Try again in a moment.';
	// Otherwise the rules of the world do not take a message now. Show what they say is happening.
	return `Not now: ${node.now?.text ?? 'it is not your turn'}.`;
}

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: arena } });
	const [text, setText] = useState('');
	const [note, setNote] = useState('');

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

	async function onSubmit(event) {
		event.preventDefault();
		const result = await world.send(text);
		// 'not-allowed' means that nothing was sent. Tell the person why, and keep what they wrote.
		if (result === 'not-allowed') {
			setNote(whyNotAllowed(world.get(), text));
			return;
		}
		setNote(result === 'sent' ? '' : 'Not delivered.');
		setText('');
	}

	const last = world.messages.filter((message) => !message.fromMe).at(-1);
	return (
		<>
			<p>{world.now?.text}</p>
			<form onSubmit={onSubmit}>
				<input placeholder="Your move" value={text} onChange={(event) => setText(event.target.value)} />
				<button>Send</button>
			</form>
			<p>{note}</p>
			<p>{last ? last.text : ''}</p>
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// While you build, a fake game answers every move.
const arena = {
	name: 'Arena',
	now: { text: 'Your move, white' },
	onMessage: (message) => `Move taken: ${message.text}`,
};

// Says why the world did not take a message, in the person's words.
function whyNotAllowed(node, text) {
	if (!text.trim()) return 'Write your move first.';
	if (!node.isOnline) return 'The world cannot be reached now. Try again in a moment.';
	// Otherwise the rules of the world do not take a message now. Show what they say is happening.
	return `Not now: ${node.now?.text ?? 'it is not your turn'}.`;
}

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

const now = document.createElement('p');
const form = document.createElement('form');
const input = document.createElement('input');
input.placeholder = 'Your move';
const send = document.createElement('button');
send.textContent = 'Send';
form.append(input, send);
const note = document.createElement('p');
const answer = document.createElement('p');
document.body.append(now, form, note, answer);

form.onsubmit = async (event) => {
	event.preventDefault();
	const text = input.value;
	const result = await world.send(text);
	// 'not-allowed' means that nothing was sent. Tell the person why, and keep what they wrote.
	if (result === 'not-allowed') {
		note.textContent = whyNotAllowed(world.get(), text);
		return;
	}
	note.textContent = result === 'sent' ? '' : 'Not delivered.';
	input.value = '';
};

// Called at once, then after every change of the world.
world.subscribe((node) => {
	now.textContent = node.now?.text ?? '';
	const last = node.messages.filter((message) => !message.fromMe).at(-1);
	answer.textContent = last ? last.text : '';
});
Live, on a fake world
Open

How it works ​

  • send treats a refusal as a result and not as an error and resolves with 'not-allowed' without rejecting. Nothing was sent and nothing is sent later.
  • When the rules refuse, now.text describes what is happening in the world ("Waiting for Bob to move") and gives the best reason to show. An unreachable world has isOnline: false.
  • Disable the box while world.canSend is false (Disable the message box when the person cannot send) and still handle 'not-allowed' for a world that changes between the last render and the click.
  • message.answer resolves with 'not-allowed' for a question already answered or for values that contain no field of the form. act resolves with it for an action the rules do not offer now.
  • A 'failed' message, on the other hand, was sent and not delivered. message.retry() sends it again (Retry a failed message).

See also ​

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