Skip to content

Events

Event Registration Timing

Callbacks can be registered at any time, before or after init(). However, to receive status changes during init() (CONNECTING, SOCKET_CONNECTED, etc.), register onStatus before calling init().

  • Only one callback per type can be registered; the latest registration overwrites the previous one.
  • Callbacks are preserved after destroy(), so you don't need to re-register them on re-initialization.

onSignal(callback)

Receives signals from the server.

js
SDK.onSignal((data) => {
  console.log(data.signal, data.payload);
});
jsx
useEffect(() => {
  window.KlleonSDK.onSignal((data) => {
    console.log(data.signal, data.payload);
  });
}, []);

Callback Parameters

FieldTypeRequiredDescription
signalstringYesSignal type
payloadobjectNoAdditional data per signal
idstringYesSignal UUID
typestringYes"SESSION" | "INPUT" | "OUTPUT"

Key Signals

Conversation Flow

SignalDescriptionpayload
RESPONSE_TEXTAvatar response text{ text: string, language: string }
STT_RESULTSpeech recognition result{ text: string }
RESPONSE_PREPARINGPreparing response (just before LLM request)-
RESPONSE_STARTEDResponse started-
RESPONSE_ENDEDResponse completed-
USER_SPEECH_STARTEDSpeech segment started within STT session (mic already on)-
USER_SPEECH_STOPPEDSpeech segment ended within STT session (mic still on)-
ERRORServer error (session maintained){ type: "STT_ERROR" | "LLM_ERROR" | ... }
REJECTEDInput rejected (session maintained){ type: "STT_EMPTY" | "MODERATION" | ... }

Session Management

SignalDescriptionpayload
MATCH_WAITINGWaiting for match (for loading UI)-
SESSION_ENDEDSession ended normally-
SESSION_TIMED_OUTSession timeout-
TIME_EXHAUSTEDUsage time exhausted-
QUOTA_EXCEEDEDConcurrent connection quota exceeded-
SUSPEND_WARNEDLong idle warning-

onStatus(callback)

Receives SDK connection status changes.

js
SDK.onStatus((status) => {
  console.log('Status:', status);
  if (status === 'CONNECTED_FINISH') {
    console.log('Avatar ready');
  }
});
jsx
const [status, setStatus] = useState('IDLE');

useEffect(() => {
  window.KlleonSDK.onStatus((s) => setStatus(s));
}, []);

onError(callback)

Receives SDK internal errors and server errors.

js
SDK.onError((error) => {
  console.error(error.code, error.message);
});
jsx
useEffect(() => {
  window.KlleonSDK.onError((error) => {
    console.error(error.code, error.message);
  });
}, []);

Callback Parameters

FieldTypeRequiredDescription
codestringYesError code
messagestringNoDetailed message

Error Code Details

Initialization Errors (on init failure)

CodeTriggermessage example
SOCKET_FAILEDSDK key validation failure or WebSocket connection failure"Failed to connect: ..."
STREAMING_FAILEDStreaming connection failure"Failed to connect streaming: ..."

On init failure, onError is not called. Since init() throws an Error, handle it with try/catch.

Runtime Errors (during active connection)

CodeTriggermessage example
SOCKET_DISCONNECTED_UNEXPECTEDLYWebSocket error occurred"WebSocket disconnected unexpectedly"
STREAMING_FAILEDRemote track subscription failure"Failed to subscribe to remote track"
STREAMING_DISCONNECTED_UNEXPECTEDLYStreaming abnormal termination"Streaming disconnected: NETWORK_ERROR"

Server Errors

CodeTriggermessage example
SERVER_ERRORSERVER_ERROR signal received from serverServer message or "SERVER_ERROR"
WORKER_DISCONNECTEDServer connection termination signal received"WORKER_DISCONNECTED"

Event Registration Example

js
const SDK = window.KlleonSDK;

SDK.onStatus((status) => console.log('Status:', status));

SDK.onSignal((data) => {
  if (data.signal === 'RESPONSE_TEXT') {
    console.log('Avatar:', data.payload.text);
  }
});

SDK.onError((error) => {
  console.error(`[${error.code}] ${error.message}`);
});

SDK.init({
  sdk_key: 'YOUR_SDK_KEY',
  avatar_id: 'YOUR_AVATAR_ID',
}).catch((e) => console.error(e.message));
jsx
import { useEffect, useRef } from 'react';

function App() {
  const initialized = useRef(false);

  useEffect(() => {
    const SDK = window.KlleonSDK;

    SDK.onStatus((status) => console.log('Status:', status));

    SDK.onSignal((data) => {
      if (data.signal === 'RESPONSE_TEXT') {
        console.log('Avatar:', data.payload.text);
      }
    });

    SDK.onError((error) => {
      console.error(`[${error.code}] ${error.message}`);
    });

    SDK.init({
      sdk_key: 'YOUR_SDK_KEY',
      avatar_id: 'YOUR_AVATAR_ID',
    })
      .then(() => { initialized.current = true; })
      .catch((e) => console.error(e.message));

    return () => {
      if (initialized.current) SDK.destroy();
    };
  }, []);

  return <avatar-container style={{ width: 400, height: 600 }} />;
}