Skip to main content
Subscribes a handler function to an event so it runs only once when that event occurs. After the event fires, the handler is automatically removed and is not called again.

Syntax

instance.once(eventName, handler);

Parameters

eventName
string
required
The name of the event to subscribe to.
handler
function
required
The callback function to run once when the event occurs. Receives (event, instance) as parameters.

Returns

void
Returns a void operator.

Examples

The following examples show different ways to use once:
// Run handler only on first send
instance.once('pre:send', (event, instance) => {
    console.log('First message being sent');
    event.message.message.content = event.message.message.content.toUpperCase();
});

// Initialize something once when chat is ready
instance.once('chat:ready', (event, instance) => {
    console.log('Performing one-time initialization');
    instance.updateCustomHeaderItems([/* ... */]);
});

Do you need practical examples?

Learn how to apply the features available for embedded chat into your implementation with guidance and examples.