Why Use Event Listeners?
Custom User Experiences
Custom User Experiences
Create dynamic, responsive interactions:
- Show contextual information based on conversation topic
- Trigger UI changes based on user messages
- Provide visual feedback for AI responses
- Integrate chat with other page elements
Third-party Integrations
Third-party Integrations
Connect your chat with external systems:
- Send data to CRM or analytics platforms
- Trigger email campaigns or notifications
- Update user profiles or preferences
- Sync conversation data with support systems
Prerequisites
A website with the Chatbase embed script already installed and working.
New to Chatbase? Check out Your First Agent to get started with the embed script first.
Available Events
Listen for these events to monitor and respond to chat activity:- Message Events
- Action Events
Track conversation flow
Triggered when a user sends a messagePayload:
Use cases: Analytics, message validation, auto-suggestions
{ data: { content: string }, type: "user-message" }
Use cases: Analytics, message validation, auto-suggestions
Triggered when your AI Agent respondsPayload:
Use cases: UI updates, satisfaction surveys, follow-up actions
{ data: { content: string }, type: "assistant-message" }
Use cases: UI updates, satisfaction surveys, follow-up actions
Basic Event Listener Usage
Adding Event Listeners
Add event listeners using the simple syntax:Must be one of the event names listed in Available Event Types.
Function to be called when the event is fired. The event payload is passed as an argument.
Removing Event Listeners
Remove listeners when they’re no longer needed to prevent memory leaks:Event Management Best Practices
Troubleshooting
Events Not Firing
Events Not Firing
Problem: Event listeners aren’t being calledSolutions:
-
Check script loading: Verify that the Chatbase embed script has loaded completely before adding event listeners. The
window.chatbase
object should be available. -
Verify event names: Double-check that you’re using the correct event names. Valid events include:
user-message
,assistant-message
,tool-call
, andtool-result
. Typos in event names will prevent listeners from firing. - Check browser console: Look for JavaScript errors that might prevent your listener functions from being registered or executed properly.
Memory Leaks
Memory Leaks
Problem: Application slowing down over timeSolutions:
-
Remove unused listeners: Always remove event listeners when they’re no longer needed using
removeEventListener
with the same function reference used inaddEventListener
. - Clean up on page unload: Remove all event listeners before the user navigates away from the page to prevent memory leaks in single-page applications.
- Handle route changes: In single-page applications, ensure you clean up event listeners when routes change, not just on full page reloads.
- Use cleanup patterns: Create cleanup functions that remove all your event listeners and call them at appropriate lifecycle points in your application.
Handler Function Issues
Handler Function Issues
Problem: removeEventListener not workingSolutions:
- Avoid inline functions: Don’t use anonymous or inline functions as event handlers if you need to remove them later. Inline functions create new function references each time, making them impossible to remove.
-
Store function references: Create named functions or store function references in variables before passing them to
addEventListener
. Use the same reference when callingremoveEventListener
. -
Test removal: Verify that your event listeners are actually being removed by checking if they still fire after calling
removeEventListener
.