Events
Events can be observed in two ways, either by assigning a function to a property or by listening to custom events fired from the component element.
onMessage
- Function: (
body: Body
) =>void
- Event:
message
Body
: {message: MessageContent
,isHistory: boolean
}
Triggered when a message is sent from the user and recieved from the target service.
message
encompasses all of the message contents.
isHistory
is used to determine whether if the message is from the prepopulated history
property.
Example
- Function
- Event
chatElementRef.onMessage = (message) => { console.log(message); };
<!-- This example is for Vanilla JS and should be tailored to your framework (see Examples) -->
chatElementRef.addEventListener('new-message', (event) => { console.log(event.detail); });
onClearMessages
- Function: () =>
void
- Event:
clear-messages
Triggered when the clearMessages
method has been executed. The core purpose of this is to help track messages state.
Example
- Function
- Event
chatElementRef.onClearMessages = () => { console.log("Messages cleared"); };
<!-- This example is for Vanilla JS and should be tailored to your framework (see Examples) -->
chatElementRef.addEventListener('messages-cleared', () => { console.log("Messages cleared"); });
onComponentRender
- Function: (
chatElementRef: DeepChat
) =>void
- Event:
render
Triggered when the component has finished rendering on the browser's window.
Example
- Function
- Event
chatElementRef.onComponentRender = () => { console.log("Finished rendering"); };
<!-- This example is for Vanilla JS and should be tailored to your framework (see Examples) -->
chatElementRef.addEventListener('render', () => { console.log("Finished rendering"); });
Setting a property's value on the DeepChat element when this event is triggered can cause infinite recursion as the component will re-render every time a property value is set. Use a flag variable to prevent this - example.
onError
- Function: (error: string) =>
void
- Event:
error
Triggered when an error message appears in the chat.
Example
- Function
- Event
chatElementRef.onError = (error) => { console.log(error); };
<!-- This example is for Vanilla JS and should be tailored to your framework (see Examples) -->
chatElementRef.addEventListener('error', (event) => { console.log(event.detail); });