Subscribe to avatar lifecycle events with automatic cleanup — no useEffect, no manual off() calls.
Must be used within <AvatarProvider>.
| Prop | Type | Default | Description |
|---|---|---|---|
eventRequired | keyof AvatarEventMap | No default | The event name to subscribe to. |
handlerRequired | Function | No default | The callback invoked when the event fires. Typed per event. |
| Prop | Type | Default | Description |
|---|---|---|---|
startOptional | () => void | No default | Session connected and started successfully. |
stopOptional | () => void | No default | Session disconnected — either by calling stop() or a network drop. |
speakingOptional | () => void | No default | Avatar began speaking. |
interruptOptional | () => void | No default | Avatar speech was cut short by interrupt(). |
readyOptional | () => void | No default | Avatar has rendered on screen and is visually ready. |
errorOptional | (error: AvatarError) => void | No default | An error occurred. Check error.fatal to decide how to respond. |
noticeOptional | (notice: SessionNotice) => void | No default | A graceful, server-initiated message (plan limit, demo cap, expiring test key). The avatar speaks it and the session then closes — not an error. |
transcriptOptional | (entries: TranscriptEntry[]) => void | No default | The whole transcript, re-emitted on every change. Only fires when transcript.enabled is set. |
healthOptional | (health: ConnectionHealth) => void | No default | Connection health, re-emitted whenever the server reports stream health. |
The handler is re-read on every render, so you can close over current props and state without re-subscribing. You still need it to be a stable behaviour — don't rely on the identity of the function you pass.
| Feature | useAvatarEvent | on / off |
|---|---|---|
| Auto-cleanup on unmount | Yes | No — you must call off() |
| Multiple events per call | One hook per event | Yes — group in one useEffect |
| Use outside React lifecycle | No | Yes |
Default to useAvatarEvent — simpler and prevents memory leaks. Reach for on/off only when you need to subscribe outside the React component lifecycle.