Skip to content

React

@vrealtime/react wraps the browser client in one hook. The session closes when the component unmounts.

import { useRealtime } from '@vrealtime/react';
export function Assistant() {
const { status, state, transcript, error, connect, interrupt } = useRealtime('wss://agent.example.com', {
tools: {
set_accent_color: {
parameters: { type: 'object', properties: { color: { type: 'string' } }, required: ['color'] },
run: ({ color }) => document.documentElement.style.setProperty('--accent', color),
},
},
audio: { input: 'mic', output: 'speaker' },
});
return (
<div>
<button onClick={status === 'connected' ? () => interrupt() : connect}>{status === 'connected' ? `${state} · stop` : 'Talk'}</button>
{error && <p role="alert">{error.message}</p>}
<ul>{transcript.map((line) => <li key={line.itemId} className={line.role}>{line.text}</li>)}</ul>
</div>
);
}

Everything connect() accepts, plus:

OptionMeaning
toolsApp tools. They are read through a ref, so new closures on re-render are used without reconnecting
autoConnectConnect on mount, for text-only apps. With audio, call connect() from a click instead
ValueMeaning
statusidle, connecting, connected, reconnecting, closed or error
stateThe agent’s turn state
transcriptLines of { itemId, role, text, final }, updated as the agent and user speak
errorThe last { code, message }, if any
connect()Idempotent and never rejects; failures land in status and error
disconnect(), interrupt(reason?), sendText(text)As on the client session
sessionThe underlying client session, for events the hook does not track