Features

Chat Interface Control

Open, close, and configure MuChat's website widget using its supported JavaScript methods.

Use window.$muchat after initializing the chat bubble to connect the widget to your website's buttons, language, theme, and navigation.

Methods

open()

Open the chat window. This method does not take a message or hidden-message option.

Open chat
window.$muchat.open();

close() and toggle()

Close the window or switch its state:

Window controls
window.$muchat.close();
window.$muchat.toggle();
window.$muchat.toggle("open");
window.$muchat.toggle("close");

These methods control the window. To control the launcher bubble separately, use toggleBubbleVisibility().

sendMessage(message)

Send a real user message after muchat:ready. A string opens chat by default:

Send a question
window.$muchat.sendMessage("Where can I see your pricing?");

To keep the current window state, pass an object:

Send without opening
window.$muchat.sendMessage({
  content: "Where can I see your pricing?",
  openWidget: false
});
ParameterTypeBehavior
contentstringMessage text; blank messages are ignored
openWidgetbooleanDefaults to true; false does not open or close the window

sendMessage() submits a message as the visitor. It does not set a welcome message, and openWidget: false does not hide the message from chat history. Send messages in response to a clear user action.

Before readiness, after destruction, or before required user identification, the SDK ignores the call. Its return value is not a promise of message delivery. The widget's normal validation and required-form rules still apply.

reset()

Clear the guest session cookie, close the window, and reload a fresh widget session:

Reset a guest chat
window.$muchat.reset();

Reset is ignored for verified users, before readiness, and while a reset is already running. It is not logout or deletion of stored conversations. It does not emit another muchat:ready event after the internal reload.

Runtime Options

Appearance, Language, and Position

Runtime appearance
window.$muchat.setLocale("en");
window.$muchat.setColorScheme("dark");
window.$muchat.setPosition("right");
MethodSupported values
setLocale(locale)fa, en, ar; also sets reading direction
setColorScheme(theme)light, dark; an unsupported value falls back to light
setPosition(position)left, right
toggleBubbleVisibility(value)show, hide

Invalid locale, position, or visibility values throw an error. Runtime changes do not save dashboard settings. Use the dashboard for content, colors, avatars, spacing, and initial messages.

Showing and Hiding the Bubble

Launcher visibility
window.$muchat.toggleBubbleVisibility("hide");
window.$muchat.toggleBubbleVisibility("show");

Hiding the bubble does not close an already open chat window. Provide a clearly labeled custom launcher if visitors still need to open chat. Authenticated-only access still requires successful identification before the bubble can appear.

Configuration Before Initialization

Set window.muchatSettings before calling muchatSDK.run. This example chooses the language, theme, position, and initial bubble visibility:

Initial settings
window.muchatSettings = {
  locale: "en",
  theme: "light",
  position: "right",
  bubble_visible: true
};

Changing this object after initialization does not update the running widget. Use the supported methods above or destroy and initialize it again. MuChat has no public setOptions() or resetOptions() API, and no optionsChanged event confirming a rendered override.

Examples

Custom Buttons

Load this after the buttons exist and the installation script has initialized the SDK:

Custom launchers
const helpButton = document.getElementById("help-button");
const closeButton = document.getElementById("close-button");
helpButton?.addEventListener("click", () => window.$muchat.open());
closeButton?.addEventListener("click", () => window.$muchat.close());

Use actual button elements with clear labels. muchat:opened and muchat:closed let you update your own controls when the window changes state.

Answer FAQ Questions

Register the readiness listener before the installation code calls muchatSDK.run. Run this after your FAQ buttons exist:

FAQ buttons
let chatReady = false;
window.addEventListener("muchat:ready", () => {
  chatReady = true;
});

document.querySelectorAll("button[data-question]").forEach((button) => {
  button.addEventListener("click", () => {
    if (!chatReady) return;
    window.$muchat.sendMessage(button.dataset.question);
  });
});

Set each button's data-question to the question it should submit. If authenticated-only access is enabled, user identification must also finish before sending. This example assumes the widget is not subsequently reset or replaced; track that lifecycle in your application's integration.

Contextual Help

For a visitor browsing pricing, use a visible help button that sends a pricing question when clicked.

Time-Based Reset

The SDK does not expose a reliable user/assistant message event stream for measuring chat inactivity. Do not reset based on a presumed user-message or assistant-message event. If your application owns a timed guest session, it can call reset() when that session expires, after warning the user through its own interface.

Reset After Tool Completion or Keywords

Browser tool-result and message-content callbacks are not available in the current widget. Do not interpret opening or closing chat as completion of a payment or another protected operation. Confirm the result through your own backend before applying your application's UI changes.

Reset on Navigation

Only reset when a new guest conversation is actually needed. Call this from your router's navigation callback after the widget is ready:

Reset for a new guest task
function onEnterNewTask() {
  window.$muchat.reset();
}

A reset does not erase a verified user's identity or conversation. The SDK already reports host-page navigation; reinstalling or resetting the widget on every route is unnecessary.

Widget Lifecycle

destroy() and init(options)

destroy() returns a promise that resolves after widget teardown. Await it before initializing again:

Reinitialize the widget
await window.$muchat.destroy();
window.$muchat.init({
  websiteToken: "YOUR_WEBSITE_TOKEN",
  baseUrl: "https://widget.mu.chat"
});

Destroy removes the frame, launcher, and SDK-managed listeners. It does not remove your application's window event listeners or the saved session cookie. For account changes and logout, follow Identity Verification.

switchWidget(options, configure?)

Use switchWidget when replacing one widget with another. It awaits destruction and initializes the new instance. The optional callback receives that instance:

Switch widget
await window.$muchat.switchWidget({
  websiteToken: "ANOTHER_WEBSITE_TOKEN",
  baseUrl: "https://widget.mu.chat"
}, (widget) => {
  widget.setLocale("en");
});

Finish one switch before starting another. Resolving this promise means initialization and the optional callback completed; it does not mean the new frame is ready or its user has been verified.

Controlling Without the Embed Script

A direct iframe uses a separate protocol. It does not create window.$muchat, and the widget methods above cannot be sent as arbitrary iframe messages. Use the iframe guide for its supported commands, readiness handshake, and origin checks.

Best Practices

  • Initialize once in a shared application layout.
  • Wait for readiness for message sending, and identify users before authenticated chat.
  • Keep a user's closed chat closed until they request help again.
  • Give users time to read a response before replacing or resetting their session.
  • Test custom launchers, focus, mobile layout, and account changes on your website.

Next Steps

MuChat documentation

On this page