Skip to main content
ComponentDescription
InteractiveMessageMessage containing interactive UI elements (forms, buttons, etc.)
InteractionGoalDefines the desired outcome of user interactions
InteractionRepresents a single user action on an interactive element
// Create and send an interactive form
const interactiveMessage = new CometChat.InteractiveMessage(
  receiverId,
  receiverType,
  "form",
  interactiveData
);
await CometChat.sendInteractiveMessage(interactiveMessage);

// Listen for interactive messages
CometChat.addMessageListener("LISTENER_ID", new CometChat.MessageListener({
  onInteractiveMessageReceived: (message) => console.log("Received:", message),
  onInteractionGoalCompleted: (receipt) => console.log("Goal completed:", receipt)
}));
Interactive messages embed UI elements like forms, buttons, and dropdowns directly within chat messages. Users can interact with these elements without leaving the conversation, enabling surveys, quick actions, and data collection.

InteractiveMessage

The InteractiveMessage class represents a message with embedded interactive content.
ParameterTypeDescriptionRequired
receiverIdstringUID of user or GUID of groupYes
receiverTypestringCometChat.RECEIVER_TYPE.USER or GROUPYes
messageTypestringType identifier (e.g., "form", "card")Yes
interactiveDataObjectJSON structure defining the interactive elementsYes
allowSenderInteractionbooleanWhether sender can interact with the messageNo (default: false)
interactionGoalInteractionGoalDefines when the interaction is considered completeNo (default: none)

Send an Interactive Message

Use sendInteractiveMessage() to send an interactive message.
let receiverId = "UID";
let receiverType = CometChat.RECEIVER_TYPE.USER;

let interactiveData = {
  title: "Survey",
  formFields: [
    {
      elementType: "textInput",
      elementId: "name",
      optional: false,
      label: "Name",
      placeholder: { text: "Enter your name" }
    },
    {
      elementType: "textInput",
      elementId: "age",
      optional: true,
      label: "Age",
      maxLines: 1,
      placeholder: { text: "Enter your age" }
    },
    {
      elementType: "dropdown",
      elementId: "gender",
      optional: false,
      label: "Gender",
      defaultValue: "male",
      options: [
        { label: "Male", value: "male" },
        { label: "Female", value: "female" }
      ]
    },
    {
      elementType: "Select",
      elementId: "interests",
      optional: true,
      label: "Interests",
      defaultValue: ["tech"],
      options: [
        { label: "Technology", value: "tech" },
        { label: "Sports", value: "sports" },
        { label: "Music", value: "music" }
      ]
    }
  ],
  submitElement: {
    elementType: "button",
    elementId: "submitButton",
    buttonText: "Submit",
    disableAfterInteracted: true,
    action: {
      actionType: "urlNavigation",
      url: "https://www.cometchat.com/"
    }
  }
};

let interactiveMessage = new CometChat.InteractiveMessage(
  receiverId,
  receiverType,
  "form",
  interactiveData
);

CometChat.sendInteractiveMessage(interactiveMessage).then(
  (message) => {
    console.log("Interactive message sent successfully", message);
  },
  (error) => {
    console.log("Interactive message sending failed with error:", error);
  }
);
On success, sendInteractiveMessage() returns an InteractiveMessage object.
FieldGetterReturn TypeDescription
interactiveDatagetInteractiveData()ObjectThe structured JSON data for the interactive element
interactionGoalgetInteractionGoal()InteractionGoalThe intended outcome of interacting with the message
interactionsgetInteractions()Interaction[]List of user interactions performed on the message
allowSenderInteractiongetIsSenderInteractionAllowed()booleanWhether the sender can interact with the message

Interactive Elements

The interactiveData object defines the UI elements. Common element types:
Element TypeDescription
textInputSingle or multi-line text field
dropdownSingle-select dropdown menu
SelectMulti-select checkbox group
buttonClickable button with action

Text Input

{
  elementType: "textInput",
  elementId: "name",
  optional: false,
  label: "Name",
  maxLines: 1,  // Optional: limit to single line
  placeholder: { text: "Enter text here" }
}
{
  elementType: "dropdown",
  elementId: "country",
  optional: false,
  label: "Country",
  defaultValue: "us",
  options: [
    { label: "United States", value: "us" },
    { label: "United Kingdom", value: "uk" },
    { label: "Canada", value: "ca" }
  ]
}

Checkbox (Multi Select)

{
  elementType: "Select",
  elementId: "preferences",
  optional: true,
  label: "Preferences",
  defaultValue: ["email"],
  options: [
    { label: "Email notifications", value: "email" },
    { label: "SMS notifications", value: "sms" },
    { label: "Push notifications", value: "push" }
  ]
}

Submit Button

{
  elementType: "button",
  elementId: "submitBtn",
  buttonText: "Submit",
  disableAfterInteracted: true,
  action: {
    actionType: "urlNavigation",
    url: "https://example.com/submit"
  }
}

Interaction Goals

An InteractionGoal defines when the user’s interaction with the message is considered complete. Use this to track engagement and trigger follow-up actions.
Goal TypeConstantDescription
Any InteractionCometChat.GoalType.ANY_ACTIONComplete when any element is interacted with
Any of SpecificCometChat.GoalType.ANY_OFComplete when any of the specified elements is interacted with
All of SpecificCometChat.GoalType.ALL_OFComplete when all specified elements are interacted with
NoneCometChat.GoalType.NONENever considered complete (default)

Set an Interaction Goal

let elementIds = ["name", "gender", "submitButton"];
let interactionGoal = new CometChat.InteractionGoal(
  CometChat.GoalType.ALL_OF,
  elementIds
);

let interactiveMessage = new CometChat.InteractiveMessage(
  receiverId,
  receiverType,
  "form",
  interactiveData
);
interactiveMessage.setInteractionGoal(interactionGoal);

CometChat.sendInteractiveMessage(interactiveMessage).then(
  (message) => {
    console.log("Interactive message sent successfully", message);
  },
  (error) => {
    console.log("Interactive message sending failed with error:", error);
  }
);

Interactions

An Interaction represents a single user action on an interactive element.
PropertyTypeDescription
elementIdstringIdentifier of the interacted element
interactedAtnumberUnix timestamp of the interaction

Mark as Interacted

Use markAsInteracted() to record when a user interacts with an element.
let messageId = 123;
let elementId = "submitButton";

CometChat.markAsInteracted(messageId, elementId).then(
  (response) => {
    console.log("Marked as interacted successfully", response);
  },
  (error) => {
    console.log("Failed to mark as interacted:", error);
  }
);

Real-Time Events

Register a MessageListener to receive interactive message events.

Receive Interactive Messages

let listenerID = "UNIQUE_LISTENER_ID";

CometChat.addMessageListener(
  listenerID,
  new CometChat.MessageListener({
    onInteractiveMessageReceived: (message) => {
      console.log("Interactive message received", message);
      // Render the interactive UI based on message.getInteractiveData()
    }
  })
);

Interaction Goal Completed

Triggered when a user’s interactions satisfy the defined InteractionGoal.
let listenerID = "UNIQUE_LISTENER_ID";

CometChat.addMessageListener(
  listenerID,
  new CometChat.MessageListener({
    onInteractionGoalCompleted: (receipt) => {
      console.log("Interaction goal completed", receipt);
      // Handle goal completion (e.g., show confirmation, trigger next step)
    }
  })
);
Always remove listeners when they’re no longer needed to prevent memory leaks.
CometChat.removeMessageListener("UNIQUE_LISTENER_ID");

Allow Sender Interaction

By default, the sender cannot interact with their own interactive message. Enable sender interaction:
let interactiveMessage = new CometChat.InteractiveMessage(
  receiverId,
  receiverType,
  "form",
  interactiveData
);
interactiveMessage.setIsSenderInteractionAllowed(true);

CometChat.sendInteractiveMessage(interactiveMessage).then(
  (message) => {
    console.log("Interactive message sent successfully", message);
  },
  (error) => {
    console.log("Interactive message sending failed with error:", error);
  }
);

Next Steps

Send Messages

Send text, media, and custom messages

Receive Messages

Listen for incoming messages in real time

Transient Messages

Send ephemeral messages that aren’t stored

Message Structure

Understand message types and hierarchy