Events
NLUX
emits events when the user interacts with the UI, when server sends messages, or
when errors occur. You can register event callbacks to listen to these events.
Listening to Events
You can register event callbacks to listen to events emitted by NLUX
.
Adding Event Callbacks
- 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 callback function that
will be called when the event is triggered. Here is an example of how to listen to the messageSent
and messageReceived
events:
import {useCallback} from 'react';
import {AiChat, EventsConfig, MessageReceivedCallback, MessageSentCallback} from '@nlux/react';
// We define an event callback using the `useCallback` hook
const messageReceivedCallback = useCallback<MessageReceivedCallback>((eventDetails) => {
console.log('Message received:', eventDetails.message);
}, [/* Your callback code dependencies */]);
const messageSentCallback = useCallback<MessageSentCallback>((eventDetails) => {
console.log('Message sent:', eventDetails.message);
}, [/* Your callback code dependencies */]);
const eventCallbacks: EventsConfig = {
messageReceived: messageReceivedCallback,
messageSent: messageSentCallback
};
<AiChat adapter={adapter} events={eventCallbacks} />
You can only register one event callback for each event.
In Vanilla JavaScript, the on
method is used to register event callbacks.
Here is an example of how to use it to listen to the messageReceived
:
You can either call on
via the AiChat
instance before mounting, when it's being created:
import {createAiChat, MessageReceivedCallback} from '@nlux/core';
const messageReceivedCallback: MessageReceivedCallback = (ev) => {
console.log('Message received:', ev.message);
};
const aiChat = createAiChat()
.withAdapter(adapter)
.on('messageReceived', messageReceivedCallback);
You can also add event callbacks anytime after it's mounted:
const aiChat = createAiChat().withAdapter(adapter);
aiChat.mount(rootElement);
// ...
aiChat.on('messageReceived', callback);
In Vanilla JavaScript, multiple event callbacks can be registered for the same event:
aiChat.on('messageReceived', callback1);
aiChat.on('messageReceived', callback2);
You cannot call on
after unmouting the AiChat
component. You can use the status
property to ensure that the
component has not been unmounted.
Removing Event Callbacks
- React JS ⚛️
- JavaScript 🟨
In order to remove an event callback, you can simply update the object passed to events
property of <AiChat />
component. For example, if you want to remove the messageSent
event listener, you can pass a new object that
does not contain the messageSent
key:
import {useCallback} from 'react';
import {AiChat, EventsConfig, MessageReceivedCallback} from '@nlux/react';
// We define an event callback using the `useCallback` hook
const messageReceivedCallback: MessageReceivedCallback = useCallback((event) => {
console.log('Message received:', event.message);
}, [/* Your callback code dependencies */]);
const eventCallbacks: EventsConfig = {
messageReceived: messageReceivedCallback,
// We remove the messageSent event listener by not including it in the object
};
<AiChat adapter={adapter} events={eventCallbacks} />
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={adapter} />
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 callbacks for a specific event by calling the removeAllEventListeners
:
aiChat.removeAllEventListeners('messageReceived');
Events
Below is a list of events emitted by NLUX
.
Ready
This event is emitted when the AiChat
component is mounted and ready to use.
- Event Name:
ready
- Callback Type:
type ReadyCallback<AiMsg = string> = (readyDetails: ReadyEventDetails<AiMsg>) => void;
type ReadyEventDetails<AiMsg> = {
// Props used to initialize the AiChat component (excluding event listeners and adapter)
aiChatProps: AiChatPropsInEvents<AiMsg>
}
- Usage:
- React JS ⚛️
- JavaScript 🟨
import {AiChat, ReadyCallback, ReadyEventDetails} from '@nlux/react';
import {useCallback} from 'react';
const readyCallback = useCallback<ReadyCallback>((readyDetails: ReadyEventDetails) => {
console.log('Chat is ready. Props used to initialize the chat:', readyDetails.aiChatProps);
}, [/* Callback dependencies */]);
// Provide the readyCallback callback as part of the events prop
<AiChat events={{ready: readyCallback}} adapter={adapter} />
import {createAiChat, ReadyCallback, ReadyEventDetails} from '@nlux/core';
const readyCallback: ReadyCallback = (readyEvent: ReadyEventDetails) => {
console.log('Chat is ready. Props used to initialize the chat:', readyEvent.aiChatProps);
};
// Registering the readyCallback when creating the AiChat instance
const aiChat = createAiChat().withAdapter(adapter).on('ready', readyCallback);
// Registering the readyCallback after creating the AiChat instance
// and before calling aiChat.mount()
aiChat.on('ready', readyCallback);
Pre-Destroy
This event is emitted when the AiChat
component is about to be destroyed.
It allows access to the component's state, conversation history, and props before it is destroyed.
- Event Name:
preDestroy
- Callback Type:
type PreDestroyCallback<AiMsg = string> = (preDestroyDetails: PreDestroyEventDetails<AiMsg>) => void;
type PreDestroyEventDetails<AiMsg = string> = {
aiChatProps: AiChatPropsInEvents<AiMsg>;
conversationHistory: Readonly<ChatItem<AiMsg>[]>;
}
- Usage:
- React JS ⚛️
- JavaScript 🟨
import {AiChat, PreDestroyCallback, PreDestroyEventDetails} from '@nlux/react';
import {useCallback} from 'react';
const preDestroyCallback = useCallback<PreDestroyCallback>((preDestroyDetails: PreDestroyEventDetails) => {
if (preDestroyDetails.conversationHistory > 0) {
// Do something with the conversation history before it is destroyed
}
}, [/* Callback dependencies */]);
// Provide the preDestroy callback as part of the events prop
<AiChat events={{preDestroy: preDestroyCallback}} adapter={adapter} />
import {createAiChat, PreDestroyCallback, PreDestroyEventDetails} from '@nlux/core';
const preDestroyCallback: PreDestroyCallback = (preDestroyDetails: PreDestroyEventDetails) => {
if (preDestroyDetails.conversationHistory.length > 0) {
// Do something with the conversation history before it is destroyed
}
};
// Registering the preDestroyCallback when creating the AiChat instance
const aiChat = createAiChat().withAdapter(adapter).on('preDestroy', preDestroyCallback);
// Registering the preDestroyCallback after creating the AiChat instance
// and before calling aiChat.unmount()
aiChat.on('preDestroy', preDestroyCallback);
// Unregistering the preDestroyCallback
aiChat.removeEventListener('preDestroy', preDestroyCallback);
Message Sent
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 runs successfully. For standard adapters, this means that the message was successfully sent to the backend.
- Event Name:
messageSent
- Callback Type:
type MessageSentCallback = (event: MessageSentEventDetails) => void;
type MessageSentEventDetails = {
uid: string;
message: string;
};
- Usage:
- React JS ⚛️
- JavaScript 🟨
import {AiChat, MessageSentCallback, MessageSentEventDetails} from '@nlux/react';
import {useCallback} from 'react';
const messageSentCallback = useCallback<MessageSentCallback>((event: MessageSentEventDetails) => {
console.log('Message sent:', event.message);
}, [/* Callback dependencies */]);
// Provide the messageSentCallback callback as part of the events prop
<AiChat events={{messageSent: messageSentCallback}} adapter={adapter} />
import {createAiChat, MessageSentCallback, MessageSentEventDetails} from '@nlux/core';
const messageSentCallback: MessageSentCallback = (event: MessageSentEventDetails) => {
console.log('Message sent:', event.message);
};
// Registering the messageSentCallback when creating the AiChat instance
const aiChat = createAiChat().withAdapter(adapter).on('messageSent', messageSentCallback);
// Add listener
// Registering the messageSentCallback after creating the AiChat instance
aiChat.on('messageSent', callback);
// Removing messageSentCallback
aiChat.removeEventListener('messageSent', callback);
Message Received
This event is emitted when a message is received from the server. For the case of streamed messages, this message is emitted 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. Depending on your
configuration, NLUX
may render messages in a character-by-character animation, so the message may still being rendered
when this event is emitted. You can use the messageRendered
event to listen to when the message has fully rendered
on the screen.
- Event Name:
messageReceived
- Callback Type:
type MessageReceivedCallback<AiMsg = string> = (event: MessageReceivedEventDetails<AiMsg>) => void;
type MessageReceivedEventDetails<AiMsg = string> = {
uid: string;
message: AiMsg;
};
- Usage:
- React JS ⚛️
- JavaScript 🟨
import {AiChat, MessageReceivedCallback, MessageReceivedEventDetails, } from '@nlux/react';
import {useCallback} from 'react';
const messageReceivedCallback = useCallback<MessageReceivedCallback>((eventDetails: MessageReceivedEventDetails) => {
console.log(eventDetails.message);
}, [/* Callback dependencies */]);
// Provide the messageReceived callback as part of the events prop
<AiChat events={{messageReceived: messageReceivedCallback}} adapter={adapter} />
import {createAiChat, MessageReceivedCallback, MessageReceivedEventDetails} from '@nlux/core';
const messageReceivedCallback: MessageReceivedCallback = (event: MessageReceivedEventDetails) => {
console.log('Message received:', event.message);
};
// Registering the messageReceivedCallback when creating the AiChat instance
const aiChat = createAiChat().withAdapter(adapter).on('messageReceived', messageReceivedCallback);
// Registering the messageReceivedCallback after creating the AiChat instance
aiChat.on('messageReceived', callback);
// Removing messageReceivedCallback
aiChat.removeEventListener('messageReceived', callback);
Message Rendered
- Event Name:
messageRendered
This event is emitted when the message has been fully rendered, including any animations that may be applied to the
message. This event is emitted after the messageReceived
event, and will only be emitted once per message.
- Callback Type:
type MessageRenderedCallback<AiMsg = string> = (event: MessageRenderedEventDetails<AiMsg>) => void;
type MessageRenderedEventDetails<AiMsg = string> = {
uid: string;
};
- Usage:
- React JS ⚛️
- JavaScript 🟨
import {MessageRenderedCallback, MessageRenderedEventDetails, } from '@nlux/react';
import {useCallback} from 'react';
const messageRenderedCallback = useCallback<MessageRenderedCallback>((eventDetails: MessageRenderedEventDetails) => {
// On message rendered, log the message to the console
console.log(eventDetails.uid);
}, [/* Callback dependencies */]);
// Provide the messageRendered callback as part of the events prop
<AiChat events={{messageRendered: messageRenderedCallback}} adapter={adapter} />
import {createAiChat, MessageRenderedCallback, MessageRenderedEventDetails} from '@nlux/core';
const messageRenderedCallback: MessageRenderedCallback = (event: MessageRenderedEventDetails) => {
console.log('Message rendered:', event.message);
};
// Registering the messageRenderedCallback when creating the AiChat instance
const aiChat = createAiChat().withAdapter(adapter).on('messageRendered', messageRenderedCallback);
// Registering the messageRenderedCallback after creating the AiChat instance
aiChat.on('messageRendered', callback);
// Removing messageRenderedCallback
aiChat.removeEventListener('messageRendered', callback);
Message Stream Started
This event is emitted when the server starts sending streaming a response back to the client, for when the adapter
is configured to use stream
data transfer mode. The event will be emitted when the first chunk of the message is
received, and will only be emitted once per message.
- Event Name:
messageStreamStarted
- Callback Type:
type MessageStreamStartedCallback = (event: MessageStreamStartedEventDetails) => void;
type MessageStreamStartedEventDetails = {
uid: string;
};
- Usage:
- React JS ⚛️
- JavaScript 🟨
import {AiChat, MessageStreamStartedCallback, MessageStreamStartedEventDetails, } from '@nlux/react';
import {useCallback} from 'react';
const messageStreamStartedCallback = useCallback<MessageStreamStartedCallback>((eventDetails: MessageStreamStartedEventDetails) => {
console.log(eventDetails.message);
}, [/* Callback dependencies */]);
// Provide the streamStarted callback as part of the events prop
<AiChat events={{streamStarted: messageStreamStartedCallback}} adapter={adapter} />
import {createAiChat, MessageStreamStartedCallback, MessageStreamStartedEventDetails} from '@nlux/core';
const messageStreamStartedCallback: MessageStreamStartedCallback = (event: MessageStreamStartedEventDetails) => {
console.log('Message stream started:', event.uid);
};
// Registering the messageStreamStartedCallback when creating the AiChat instance
const aiChat = createAiChat().withAdapter(adapter).on('messageStreamStarted', messageStreamStartedCallback);
// Registering the messageStreamStartedCallback after creating the AiChat instance
aiChat.on('messageStreamStarted', callback);
// Removing messageStreamStartedCallback
aiChat.removeEventListener('messageStreamStarted', callback);
Error
This event is emitted when an exception occurs. The event listener receives the ErrorEventDetails
object
as a parameter.
- Event Name:
error
- Callback Type:
type ErrorCallback = (errorDetails: ErrorEventDetails) => void;
type ErrorEventDetails = {
errorId: string;
message: string;
errorObject?: Error;
};
- Usage:
- React JS ⚛️
- JavaScript 🟨
import {AiChat, ErrorCallback, ErrorEventDetails} from '@nlux/react';
import {useCallback} from 'react';
const errorCallback = useCallback<ErrorCallback>((errorDetails: ErrorEventDetails) => {
console.log('An error occurred:', errorDetails.message);
console.log('Error ID:', errorDetails.errorId);
}, [/* Callback dependencies */]);
// Provide the errorCallback callback as part of the events prop
<AiChat events={{error: errorCallback}} adapter={adapter} />
import {createAiChat, ErrorCallback, ErrorEventDetails} from '@nlux/core';
const aiChat = createAiChat().withAdapter(adapter);
const errorCallback: ErrorCallback = (errorDetails: ErrorEventDetails) => {
console.log(errorDetails.errorId);
console.log(errorDetails.message);
};
// Registering the errorCallback when creating the AiChat instance
const aiChat = createAiChat().withAdapter(adapter).on('error', errorCallback);
// Registering the errorCallback after creating the AiChat instance
aiChat.on('error', errorCallback);
// Unregistering the errorCallback
aiChat.removeEventListener('error', errorCallback);