Features
Event Listeners
Listen for MuChat widget readiness, window state, identification, teardown, and errors.
Widget events let your website respond to the chat's lifecycle. Listen on window with standard browser event listeners; MuChat event names start with muchat: and payloads are available as event.detail.
Why Use Event Listeners?
Enable a control after widget readiness, update your custom launcher when chat opens or closes, or show an identification error in your application.
Forward opening and closing counts to your own analytics integration. Use only the fields you need; lifecycle events do not provide a complete transcript or a dependable count of user and assistant messages.
Prerequisites
Available Events
| Event | When it fires | Payload |
|---|---|---|
muchat:ready | Initial bootstrap and SDK configuration have loaded | widget, message_popup, user, customAttributes, mobileFullscreen |
muchat:opened | Window state changes to open | Empty object |
muchat:closed | Window state changes to closed | Empty object |
muchat:destroyed | SDK teardown completes | websiteToken |
ready does not mean a user has been identified or every frame update has rendered. The guest reset() flow suppresses another ready event. A new initialization after destruction has its own readiness event.
| Event | When it fires | Payload |
|---|---|---|
muchat:user-identified | Identification succeeds with authenticated-only access enabled | Empty object |
muchat:error | A bootstrap or forwarded widget operation fails | Operation-dependent error fields |
user-identified is not emitted for an unrestricted widget. Do not wait for it in that mode. For bootstrap failures, error details include action: "bootstrap", code: "WIDGET_BOOTSTRAP_FAILED", websiteToken, status, and error. Other operation payloads can differ.
Message and Action Events
The active widget does not currently emit a public user-message, assistant-message, tool-call, tool-result, form-submission, dictation-transcript, or options-changed event stream to the host page.
The SDK contains forwarding handlers for muchat:message-created and its muchat:on-message alias, but the active widget does not publish those messages to that boundary. Do not use them for message analytics, inactivity timers, keyword matching, or detecting that an API operation succeeded.
Basic Event Listener Usage
Adding Event Listeners
function onMuchatReady(event) {
console.log("Widget ready", {
mobileFullscreen: event.detail.mobileFullscreen
});
}
function onMuchatOpened() {
console.log("Chat opened");
}
function onMuchatError(event) {
const detail = event.detail || {};
console.error("Widget operation failed", {
action: detail.action,
code: detail.code
});
}
window.addEventListener("muchat:ready", onMuchatReady);
window.addEventListener("muchat:opened", onMuchatOpened);
window.addEventListener("muchat:error", onMuchatError);Register these listeners before initialization. Loading sdk.js alone does not signal readiness. Keep tokens and full contact payloads out of logs and analytics.
Removing Event Listeners
Use the same function reference used for registration:
window.removeEventListener("muchat:ready", onMuchatReady);
window.removeEventListener("muchat:opened", onMuchatOpened);
window.removeEventListener("muchat:error", onMuchatError);React Component Example
This component tracks subsequent open and close events and cleans up on unmount. Render it once in the same shared layout as the widget:
"use client";
import { useEffect, useState } from "react";
export default function ChatState() {
const [isOpen, setIsOpen] = useState(false);
useEffect(() => {
const opened = () => setIsOpen(true);
const closed = () => setIsOpen(false);
window.addEventListener("muchat:opened", opened);
window.addEventListener("muchat:closed", closed);
return () => {
window.removeEventListener("muchat:opened", opened);
window.removeEventListener("muchat:closed", closed);
};
}, []);
return <span>{isOpen ? "Chat is open" : "Chat is closed"}</span>;
}If your application can destroy or switch widgets, also reset your interface on muchat:destroyed. Register listeners before opening chat if you need its initial state.
Listening Without the Embed Script
A direct iframe has its own readiness and identity events. It does not emit the same window custom events as the floating widget. Use the connector in the iframe guide, including source-window, origin, and message-prefix checks. Do not expect a plain object with event.data.type to match that protocol.
Event Management Best Practices
- Keep listener registration in a shared integration file or component.
- Remove listeners when their owner unmounts;
destroy()does not remove your application's listeners. - Avoid duplicate registration on route changes.
- Keep handlers short and catch errors from asynchronous work they start.
- Use your backend's confirmed result for protected operations, such as payment or account changes.
Troubleshooting
Use window.addEventListener, the exact muchat: event name, and event.detail. Register before the triggering action. A listener added after ready cannot recover the missed event. Check the console and verify that the widget initialized successfully.
This event is emitted only when authenticated-only access is enabled. An unrestricted widget can identify a user without emitting it. Check the identify network response when testing that mode.
Check repeated mounts or route-level registration. Use named functions, remove them during cleanup, or use { once: true } for an event your integration needs only once.
Pass the original callback reference. Creating a new anonymous function during removal does not remove the registered function. Match the capture option as well if you supplied one.
The active widget does not provide those host events. Do not infer their availability from a handler name in the SDK. Use the supported lifecycle events and your own service's confirmed results.