connectToHost(options?)
Connects your view to the world (or agent) it belongs to and resolves to that world.
import { connectToHost } from '@unaiverse/gui-kit';
const world = await connectToHost();Parameters
options and each of its fields are optional.
| Field | What it is |
|---|---|
fake | A fake world for building with no account and no network, written as { world: { ... } } or as { agent: { ... } } for an agent (Fake worlds). Leave it out when your view runs in the webapp. |
webapp | With fake, the look of the webapp that your view receives in place of the one taken from your page (for example { theme: 'dark', language: 'it' }). Without this field the fake world takes the theme and the language from your page (GuiWebapp). |
timeoutMs | The milliseconds to wait for the webapp (5000 by default). |
hostOrigins | The origins of the webapp that your view accepts (DEFAULT_HOST_ORIGINS by default). |
Returns
A Promise that resolves to the world. The object holds its name, its data, the messages, the current state of its rules (now), the person using your view (me) and the methods that send messages and choose actions. The Promise resolves once the webapp has accepted your view and sent it the first state of the world.
Usage
In the webapp
Call it once on start and render the world in a subscribe listener:
const world = await connectToHost();
world.subscribe((node) => draw(node));While you build: a fake world
With fake the kit builds the world inside your page and needs no account, no webapp and no network:
const world = await connectToHost({
fake: {
world: {
name: 'Arena',
data: { board: [[0, 1], [1, 0]] },
me: { nickname: 'ada' },
onMessage: (message) => `You said ${message.text}`,
},
},
});Removing fake switches to the real webapp without any other change to your code.
The view of an agent
The view of an agent replaces the chat between the person and the agent in the webapp. Build it with a fake agent:
const agent = await connectToHost({ fake: { agent: { name: 'Helper', onMessage: () => 'Hi!' } } });In the webapp connectToHost() takes no agent name and resolves to the agent that owns the view.
Try another look of the webapp
While building, this call shows your view in the dark theme and in Italian:
const world = await connectToHost({ fake: { world }, webapp: { theme: 'dark', language: 'it' } });React
In React the useWorld hook calls connectToHost once and returns the newest world to your component.
Caveats
- Call it once per page. Each call opens a new connection to the webapp that replaces the previous one.
- Your page must be inside the webapp. A page opened on its own in a browser tab has no world and the call rejects with
'no-host'. Build withfake. To try your view in the webapp, open the card of one of your worlds under You and write the address of your view in its Own view field (Going live). - Connect within 20 seconds. If your page has not connected 20 seconds after the webapp loaded it, the webapp replaces it with the UNaIVERSE view of the world and shows a notice with a retry button. Call
connectToHost()on start before loading images or fonts.
Troubleshooting
| Code | Why | What to do |
|---|---|---|
'no-host' | The page is not inside the webapp or the webapp did not answer within timeoutMs. | Show a short message and a link to the world in UNaIVERSE. While building, use fake. |
'unsupported' | The webapp does not accept the version of your kit. | Use the kit from this site or from its GitHub repository (Installation). Both use version 1 of the webapp protocol. |
The code of the error tells the two apart:
try {
const world = await connectToHost();
} catch (error) {
if (error.code === 'no-host') showOpenInUnaiverse();
else showError(error.code);
}DEFAULT_HOST_ORIGINS
Without hostOrigins a view accepts the webapp at the origins 'https://unaiverse.io' and 'http://localhost:5173' (the webapp running on the computer of a developer).
import { connectToHost, DEFAULT_HOST_ORIGINS } from '@unaiverse/gui-kit';
// Also accept a test copy of the webapp.
const world = await connectToHost({ hostOrigins: [...DEFAULT_HOST_ORIGINS, 'https://test.unaiverse.io'] });