Skip to main content
Subscribes a handler function to a specific chat event. The handler will be called every time the event occurs.

Syntax

instance.on(eventName, handler);

Parameters

eventName
string
required
The name of the event to subscribe to, for example, 'send', 'receive', 'chat:ready'.
handler
function
required
The callback function to run when the event occurs. Receives (event, instance) as parameters.

Returns

void
Returns a void operator.

Examples

The following examples show different ways to subscribe to events:
// Subscribe to send event
instance.on('send', (event, instance) => {
    console.log('Message sent:', event);
});

// Subscribe to receive event
instance.on('receive', (event, instance) => {
    console.log('Message received:', event);
});

// Subscribe to chat ready event
instance.on('chat:ready', (event, instance) => {
    console.log('Chat is ready');
    instance.send('Hello!');
});

Considerations

Common Events

  • chat:ready
    Fired when the chat is fully loaded and ready.
  • pre:send
    Fired before a message is sent.
  • send
    Fired after a message is sent.
  • pre:receive
    Fired before a message is received.
  • receive
    Fired after a message is received.
  • feedback
    Fired when user provides feedback.
  • userDefinedResponse
    Fired for custom response types.
  • pre:restartConversation
    Fired before conversation restart.
  • restartConversation
    Fired after conversation restart.
  • pre:threadLoaded
    Fired before a thread is loaded from history.
  • authTokenNeeded
    Fired when authentication token needs refresh.
  • view:pre:change
    Fired before view state changes.
  • view:change
    Fired after view state changes.
For more details about events, see Event categories.

Do you need practical examples?

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