Events
NLUX
emits events when the user interacts with the UI, when server sends a message, or
when errors occur. You can register event listeners to listen to these events.
- React JS ⚛️
- JavaScript 🟨
Event Listeners
You can register event listeners to listen to events emitted by the AI chat UI component.
Events are emitted when the user interacts with the UI, when server sends a message, or when errors occur.
Listen To An Event
- react-js
- javascript
You can listen to an event by using the events
property when defining your <AiChat />
component.
The events
property is an object where the key is the event name and the value is a function that will be called when the event is triggered.
Here is an example of how to listen to the messageSent
and messageReceived
events:
<AiChat
adapter={myAdapter}
events={{
messageSent: (message: string) => console.log(message),
messageReceived: (message: string) => console.log(message),
}}
/>
In Vanilla JS, the on
method is used to register one or multiple event listeners.
Here is an example of how to use it to listen to the messageReceived
event:
You can either call on
via the AiChat
instance before mounting it to the DOM, when you create it:
const aiChat = createAiChat()
.withAdapter(myAdapter)
.on('messageReceived', callback);
Or anytime after mounting it to the DOM:
const aiChat = createAiChat().withAdapter(myAdapter);
aiChat.mount(rootElement);
aiChat.on('messageReceived', callback);
In Vanilla Javascript, multiple event listeners can be registered for the same event:
aiChat.on('messageReceived', callback1);
aiChat.on('messageReceived', callback2);
Remove An Event Listener
- react-js
- javascript
In order to remove an event listener, you can simply update the object passed to events
property of <AiChat />
component. For example, if you want to remove the messageReceived
event listener, you can pass a new object that
does not contain the messageReceived
key:
<AiChat
adapter={myAdapter}
events={{
messageSent: (message: string) => console.log(message),
}}
/>
Whenever the events
property is updated, <AiChat />
component will automatically remove the previous event listeners
and add the new ones.
If you want to remove all event listeners, you can simply remove the events
property:
<AiChat adapter={myAdapter} />
You can remove a specific event listener by calling the removeEventListener
:
- The first argument is the event name
- The second argument is the callback function to remove
aiChat.removeEventListener('messageReceived', callback);
You can also remove all the event listeners for a specific event by calling the removeAllEventListeners
:
aiChat.removeAllEventListeners('messageReceived');
If no event name is provided, all the event listeners for all the events will be removed:
aiChat.removeAllEventListeners();
Events
Below is a list of events emitted by the AI chat UI component.
Ready
- Event Name:
ready
This event is emitted when the AI chat UI component is mounted and ready to use.
- Callback Type:
type ReadyEventDetails = {
// Props used to initialize the AiChat component
aiChatProps: AiChatProps;
};
type ReadyCallback = (readyDetails: ReadyEventDetails) => void;
- Example:
- React JS ⚛️
- JavaScript 🟨
import {AiChat, ReadyCallback, ReadyEventDetails} from '@nlux/react';
export default () => {
const callback = useCallback<ReadyCallback>(
(readyDetails: ReadyEventDetails) => {
// Do something
}, []
);
return (
<AiChat
adapter={adapter}
events={{
ready: callback,
}}
/>
);
};
import {createAiChat, ReadyCallback, ReadyEventDetails} from '@nlux/core';
const callback: ReadyCallback = (readyDetails: ReadyEventDetails) => {
// Do something
};
const aiChat = createAiChat().withAdapter(myAdapter);
aiChat.on('ready', callback);
Pre-Destroy
- Event Name:
preDestroy
This event is emitted when the AI chat UI component is about to be destroyed. It allows access to the component's state and props before it is destroyed.
- Callback Type:
type PreDestroyEventDetails = {
aiChatProps: AiChatProps;
conversationHistory: Readonly<ConversationItem[]>;
};
type PreDestroyCallback = (preDestroyDetails: PreDestroyEventDetails) => void;
- Example:
- React JS ⚛️
- JavaScript 🟨
import {AiChat, PreDestroyCallback, PreDestroyEventDetails} from '@nlux/react';
export default () => {
const callback = useCallback<ReadyCallback>(
(preDestroyDetails: PreDestroyEventDetails) => {
if (preDestroyDetails.conversationHistory > 0) {
// Do something with the conversation history before it is destroyed
}
}, []
);
return (
<AiChat
adapter={adapter}
events={{
ready: callback,
}}
/>
);
};
import {createAiChat, PreDestroyCallback, PreDestroyEventDetails} from '@nlux/core';
const callback: PreDestroyCallback = (preDestroyDetails: PreDestroyEventDetails) => {
if (preDestroyDetails.conversationHistory.length > 0) {
// Do something with the conversation history before it is destroyed
}
};
const aiChat = createAiChat().withAdapter(myAdapter);
// Register the callback
aiChat.on('preDestroy', callback);
// Unregister the callback
aiChat.removeEventListener('preDestroy', callback);
Message Sent
- Event Name:
messageSent
This event is emitted when the user sends a message. The event only gets triggered when the adapter method responsible for sending the message to the backend executes successfully. For standard adapters, this means that the message was successfully sent to the backend.
- Callback Type:
type MessageSentCallback = (message: string) => void;
- Example:
- React JS ⚛️
- JavaScript 🟨
import {AiChat, MessageSentCallback} from '@nlux/react';
export default () => {
const messageSentCallback = useCallback<MessageSentCallback>(
(message: string) => console.log(message), []
);
return (
<AiChat
adapter={adapter}
events={{
messageSent: messageSentCallback,
}}
/>
);
};
import {createAiChat, MessageSentCallback} from '@nlux/core';
const aiChat = createAiChat().withAdapter(myAdapter);
const callback: MessageSentCallback = (message: string) => console.log(message);
// Add listener
aiChat.on('messageSent', callback);
// Remove listener
aiChat.removeEventListener('messageSent', callback);
Message Received
- Event Name:
messageReceived
This event is emitted when a message is received from the server. The event listener receives the message as a parameter.
| For Promise Adapters ― The event listener receives when the promise
is resolved.
| For Streaming Adapters ― The event listener receives the message
when the streaming is complete and the message is fully received.
Please note that messageReceived
does not mean that the message has fully rendered on the screen.
NLUX
renders the messages in a character-by-character animation, so the message may still being
rendered when this event is emitted.
- Callback Type:
type MessageReceivedCallback = (message: string) => void;
- Example:
- React JS ⚛️
- JavaScript 🟨
import {AiChat, MessageSentCallback} from '@nlux/react';
export default () => {
const messageReceivedCallback = useCallback<MessageSentCallback>(
(message: string) => console.log(message), []
);
return (
<AiChat
adapter={adapter}
events={{
messageReceived: messageReceivedCallback,
}}
/>
);
};
import {createAiChat, MessageReceivedCallback} from '@nlux/core';
const aiChat = createAiChat().withAdapter(myAdapter);
const callback: MessageReceivedCallback = (message: string) => console.log(message);
// Add listener
aiChat.on('messageReceived', callback);
// Remove listener
aiChat.removeEventListener('messageReceived', callback);
Error
- Event Name:
error
This event is emitted when an exception occurs. The event listener receives the ErrorEventDetails
object
as a parameter.
- Callback Type:
type ErrorEventDetails = {
errorId: string;
message: string;
};
type ErrorCallback = (error: ErrorEventDetails) => void;
- Example:
- React JS ⚛️
- JavaScript 🟨
import {AiChat, ErrorCallback, ErrorEventDetails} from '@nlux/react';
export default () => {
const errorCallback = useCallback<ErrorCallback>(
(details: ErrorEventDetails) => {
console.log(details.errorId);
console.log(details.message);
}, []
);
return (
<AiChat
adapter={adapter}
events={{
error: errorCallback,
}}
/>
);
};
import {createAiChat, ErrorCallback, ErrorEventDetails} from '@nlux/core';
const aiChat = createAiChat().withAdapter(myAdapter);
const callback: ErrorCallback = (details: ErrorEventDetails) => {
console.log(details.errorId);
console.log(details.message);
};
// Add listener
aiChat.on('error', callback);
// Remove listener
aiChat.removeEventListener('error', callback);