Skip to main content
The widget emits events that let you respond to user interactions and chat activity in your application.

Configuration callbacks

The simplest way to handle events is through callback functions in your configuration:
const voltai = Voltai.load({
  apiKey: 'your-api-key',
  onReady: () => {
    console.log('Widget is ready');
  },
  onOpen: () => {
    console.log('Widget opened');
  },
  onClose: () => {
    console.log('Widget closed');
  }
});
CallbackDescription
onReadyCalled when the widget has initialized and authentication is complete
onOpenCalled when the chat panel opens
onCloseCalled when the chat panel closes

Event listener API

For more control, use the on() and off() methods to subscribe to events dynamically.

on(event, handler)

Subscribe to an event.
const unsubscribe = voltai.on('widget:open', () => {
  console.log('Chat opened');
});
Returns: A function to remove the listener.

off(event, handler?)

Remove event listeners.
// Remove a specific handler
voltai.off('widget:open', myHandler);

// Remove all handlers for an event
voltai.off('widget:open');

Available events

Widget events

EventPayloadDescription
widget:readyWidget initialized and ready
widget:openChat panel opened
widget:closeChat panel closed

Chat events

EventPayloadDescription
chat:send{ message, chatId, topics? }Message sent by user
chat:complete{ chatId, success }Chat response completed

Event payloads

chat:send

Fired when a message is sent.
voltai.on('chat:send', (payload) => {
  console.log('Message:', payload.message);
  console.log('Chat ID:', payload.chatId);
  console.log('Topics:', payload.topics);
});
PropertyTypeDescription
messagestringThe message text
chatIdstringUnique identifier for the conversation
topicsstring[]Topics associated with the message (optional)

chat:complete

Fired when a response has finished.
voltai.on('chat:complete', (payload) => {
  if (payload.success) {
    console.log('Response received for chat:', payload.chatId);
  } else {
    console.log('Chat failed:', payload.chatId);
  }
});
PropertyTypeDescription
chatIdstringThe conversation identifier
successbooleanWhether the response completed successfully

Example: Analytics integration

Track widget usage with your analytics provider:
const voltai = Voltai.load({
  apiKey: 'your-api-key',
  onOpen: () => {
    analytics.track('Widget Opened');
  },
  onClose: () => {
    analytics.track('Widget Closed');
  }
});

voltai.on('chat:send', (payload) => {
  analytics.track('Chat Message Sent', {
    chatId: payload.chatId,
    topics: payload.topics
  });
});

voltai.on('chat:complete', (payload) => {
  analytics.track('Chat Response Received', {
    chatId: payload.chatId,
    success: payload.success
  });
});

Example: Cleanup on unmount

In a single-page app, clean up listeners when navigating away:
// Subscribe
const unsubscribeOpen = voltai.on('widget:open', handleOpen);
const unsubscribeSend = voltai.on('chat:send', handleSend);

// Later, when unmounting
unsubscribeOpen();
unsubscribeSend();
voltai.destroy();
The on() method returns an unsubscribe function. Store it if you need to remove the listener later.