Skip to main content

Event Handling

Klleon Chat SDK uses custom events to notify your application of real-time state changes and data. By subscribing to these events, you can build dynamic experiences aligned with the SDK’s behavior.

Registering and Managing Event Listeners

KlleonChat.onChatEvent(callback)

Registers a callback function that is triggered whenever a chat message related to the avatar or user is received. This allows you to build a custom chat UI without relying on the SDK’s <chat-container> component.

  • callback ((data: ChatData) => void, required): A function that receives a ChatData object.
function handleChatMessage(chatData) {
console.log("New chat message:", chatData);
}

window.KlleonChat.onChatEvent(handleChatMessage);

ChatData Object and ResponseChatType Details

The properties of the ChatData object passed to onChatEvent are:

Property NameTypeDescription
message string Content of the received message.
chat_type string (ResponseChatType) Indicates the type of message. For possible values, refer to the 'ResponseChatType Values Detail' tab.
time string String representation of the time when the message occurred. (e.g., ISO 8601 format)
id string Unique identifier of the message.

ChatData Flow

Sending a Text Message

Sending a Voice Message


KlleonChat.onStatusEvent(callback)

Registers a callback function that is triggered when the SDK or avatar’s internal status changes.

  • callback ((status: Status) => void, required): A function receiving the current status as a string.
function handleSdkStatus(currentStatus) {
console.log("SDK Status Changed:", currentStatus);

if (currentStatus === "VIDEO_CAN_PLAY") {
console.log("Avatar video is ready! You can now use other SDK methods.");
}
}

window.KlleonChat.onStatusEvent(handleSdkStatus);
SDK Method Usage Prerequisite

Most SDK methods (e.g., sending messages, STT functions) must be called only when the avatar is fully connected and in a VIDEO_CAN_PLAY state. This state is communicated through onStatusEvent. Calling methods before this state may lead to unexpected behavior.

KlleonChat.init() and event listener methods (onChatEvent, onStatusEvent) are exceptions to this rule.

Status Event Arguments

TypeDescription
string (Status) String representing the current status of the SDK and avatar. For possible values, refer to the 'Status Values Detail' table below.

Available Status Values

Status ValueDescription
IDLE SDK is initialized but not yet connected, in idle state.
CONNECTING Status after validating and passing sdk_key and avatar_id before session connection.
CONNECTING_FAILED Status when validation failed due to incorrect sdk_key or avatar_id values.
SOCKET_CONNECTED Status when successfully connected to the WebSocket server.
SOCKET_FAILED Status when WebSocket server connection failed.
STREAMING_CONNECTED Status when successfully connected to the media streaming (WebRTC) server.
STREAMING_FAILED Status when media streaming (WebRTC) server connection failed.
CONNECTED_FINISH Status when all connections (WebSocket, streaming) are successfully completed.
VIDEO_LOAD Status when avatar video data loading has started.
VIDEO_CAN_PLAY Avatar video is ready to play and most SDK methods can be called in this prepared state.

Status Event Flow

Re-registering Callbacks

If onChatEvent or onStatusEvent is called multiple times for the same event type, only the most recently registered callback remains active. Older callbacks are automatically removed. There’s no explicit off method.