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?

Prerequisites

Install the chat bubble. Register initial readiness listeners before the installation code calls window.muchatSDK.run(...); events are not replayed for listeners registered later.

Available Events

EventWhen it firesPayload
muchat:readyInitial bootstrap and SDK configuration have loadedwidget, message_popup, user, customAttributes, mobileFullscreen
muchat:openedWindow state changes to openEmpty object
muchat:closedWindow state changes to closedEmpty object
muchat:destroyedSDK teardown completeswebsiteToken

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.

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

Observe lifecycle events
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:

Remove listeners
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:

ChatState.jsx
"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

Next Steps

MuChat documentation

On this page