Show whether a message was delivered
Delivering a message takes a moment and can fail. Each message of the person has a status that tracks its delivery and that your view can render as a word next to the message. In the example the fake agent fails the first message on purpose and delivers the second.
jsx
import { useState } from 'react';
import { useWorld } from './useWorld.js';
// The fake agent fails the first message sent to it, because of `failNext`.
const courier = {
name: 'Courier',
onMessage: () => 'Received.',
failNext: true,
};
// The `status` of a message from the person is 'sending', 'sent' or 'failed'.
const DELIVERY = { sending: 'Sending…', sent: 'Delivered', failed: 'Not delivered' };
export default function App() {
// While building: the fake agent. In the webapp: useWorld()
const { world: agent, error } = useWorld({ fake: { agent: courier } });
const [text, setText] = useState('');
if (error) return <p>Could not connect ({error.code}).</p>;
if (!agent) return <p>Connecting…</p>;
function handleSubmit(event) {
event.preventDefault();
if (!text.trim()) return;
agent.send(text);
setText('');
}
return (
<>
{agent.messages.map((message) => (
<p key={message.id}>
<b>{message.fromMe ? 'You' : message.from}:</b> {message.text}
{message.fromMe && <small> ({DELIVERY[message.status]})</small>}
</p>
))}
<form onSubmit={handleSubmit}>
<input value={text} onChange={(event) => setText(event.target.value)} placeholder="Message" />
<button disabled={!agent.canSend}>Send</button>
</form>
</>
);
}js
import { connectToHost } from '@unaiverse/gui-kit';
// The fake agent fails the first message sent to it, because of `failNext`.
const courier = {
name: 'Courier',
onMessage: () => 'Received.',
failNext: true,
};
// The `status` of a message from the person is 'sending', 'sent' or 'failed'.
const DELIVERY = { sending: 'Sending…', sent: 'Delivered', failed: 'Not delivered' };
// While building: the fake agent. In the webapp: const agent = await connectToHost();
const agent = await connectToHost({ fake: { agent: courier } });
const list = document.createElement('div');
const form = document.createElement('form');
const input = document.createElement('input');
input.placeholder = 'Message';
const sendButton = document.createElement('button');
sendButton.textContent = 'Send';
form.append(input, sendButton);
document.body.append(list, form);
form.onsubmit = (event) => {
event.preventDefault();
if (!input.value.trim()) return;
agent.send(input.value);
input.value = '';
};
agent.subscribe((node) => {
sendButton.disabled = !node.canSend;
list.replaceChildren(...node.messages.map((message) => {
const line = document.createElement('p');
const sender = document.createElement('b');
sender.textContent = `${message.fromMe ? 'You' : message.from}:`;
line.append(sender, ` ${message.text ?? ''}`);
if (message.fromMe) {
const delivery = document.createElement('small');
delivery.textContent = ` (${DELIVERY[message.status]})`;
line.append(delivery);
}
return line;
}));
});How it works
- Only the messages of the person (
fromMe) have astatus. It is'sending'while the message is pending,'sent'once it is delivered and'failed'when delivery did not succeed. DELIVERYis a table that maps each status to a word. Change the words or render an icon instead.failNext: truemakes a fake agent or world fail only the next message sent to it and lets you see the failed state while you build.message.retry()sends a failed message again (Retry a failed message).
See also
GuiMessage: every field of a message.- Retry a failed message.
- Test failures with a fake world.