Skip to main content
Quick Reference - Send an interactive message and listen for it:
// Send an interactive message
const interactiveMessage = new CometChat.InteractiveMessage(
  "UID", CometChat.RECEIVER_TYPE.USER, "form", { title: "Survey", formFields: [] }
);
await CometChat.sendInteractiveMessage(interactiveMessage);

// Listen for incoming interactive messages
CometChat.addMessageListener("interactive-listener", new CometChat.MessageListener({
  onInteractiveMessageReceived: (msg) => console.log("Interactive:", msg),
  onInteractionGoalCompleted: (msg) => console.log("Goal completed:", msg),
}));
Available via: SDK | REST API
An InteractiveMessage is a specialized object that encapsulates an interactive unit within a chat message, such as an embedded form that users can fill out directly within the chat interface. This enhances user engagement by making the chat experience more interactive and responsive to user input.

InteractiveMessage

InteractiveMessage is a chat message with embedded interactive content. It can contain the following properties:
ParameterDescription
receiverIdThe UID or GUID of the recipientRequired
receiverTypeThe type of the receiver to whom the message is to be sent i.e CometChat.RECEIVER_TYPE.USER (user) or CometChat.RECEIVER_TYPE.GROUP (group)Required
messageTypeThe type of the message that needs to be sentRequired
interactiveDataA JSONObject holding structured data for the interactive elementRequired
allowSenderInteractionA boolean determining whether the message sender can interact with the message by default it is set to false
interactionGoalAn InteractionGoal object encapsulating the intended outcome of interacting with the InteractiveMessage by default it is set to none

Interaction

An Interaction represents a user action involved with an InteractiveMessage. It includes:
  • elementId: An identifier for a specific interactive element.
  • interactedAt: A timestamp indicating when the interaction occurred.

Mark as Interacted

This method marks a message as interacted by identifying it with the provided Id. It also logs the interactive element associated with the interaction.
CometChat.markAsInteracted(message?.getId(), elementId)
  .then((response) => {
    console.log("Mark As Interacted", response);
  })
  .catch((error) => {
    console.log("error while markAsInteracted", error);
  });
On SuccessmarkAsInteracted() returns a success message:
ParameterTypeDescriptionSample Value
messagestringConfirmation message"The message id 25308 has been marked as interacted for the user cometchat-uid-7."

Goal Completion

A key feature of InteractiveMessage is checking whether a user’s interactions with the message meet the defined InteractionGoal. You would be tracking every interaction users perform on an InteractiveMessage (captured as Interaction objects) and comparing those with the defined InteractionGoal. The completion of a goal can vary depending on the goal type:
GoalsDescriptionKeys
Any InteractionThe goal is considered completed if there is at least one interaction.CometChat.GoalType.ANY_ACTION
Any of Specific InteractionsThe goal is achieved if any of the specified interactions occurred.CometChat.GoalType.ANY_OF
All of Specific InteractionsThe goal is completed when all specified interactions occur.CometChat.GoalType.ALL_OF
NoneThe goal is never completedCometChat.GoalType.NONE
This user interaction tracking mechanism provides a flexible and efficient way to monitor user engagement within an interactive chat session. By defining clear interaction goals and checking user interactions against these goals, you can manage user engagement and improve the overall chat experience in your CometChat-enabled application. InteractionGoal The InteractionGoal represents the desired outcome of an interaction with an InteractiveMessage. It includes:
  • elementIds: A list of identifiers for the interactive elements.
  • type: The type of interaction goal from the CometChat.

Sending InteractiveMessages

The InteractiveMessage can be sent using the sendInteractiveMessage method of the CometChat class. The method requires an InteractiveMessage object and a CallbackListener for handling the response. Here is an example of how to use it:
CometChat.sendInteractiveMessage(message)
  .then((message) => {
    console.log("message sent successfully", message.getSentAt());
  })
  .catch((error) => {
    console.log("error while sending message", { error });
  });
On SuccesssendInteractiveMessage() returns the sent interactive message object:InteractiveMessage Object:
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25308"
conversationIdstringConversation identifier"cometchat-uid-6_user_cometchat-uid-7"
receiverIdstringReceiver’s UID"cometchat-uid-7"
receiverTypestringType of receiver"user"
typestringMessage type"form"
categorystringMessage category"interactive"
sentAtnumberUnix timestamp when sent1771998776
updatedAtnumberUnix timestamp when updated1771998776
interactiveDataobjectInteractive element dataSee below ↓
interactionGoalobjectGoal for tracking interactionsSee below ↓
senderobjectSender user detailsSee below ↓
receiverobjectReceiver user detailsSee below ↓
dataobjectAdditional message dataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

interactiveData Object:
ParameterTypeDescriptionSample Value
titlestringForm title"Quick Survey"
formFieldsarrayForm field definitionsSee below ↓
submitElementobjectSubmit button configurationSee below ↓
interactableElementIdsarrayIDs of interactable elements["submit_btn"]

interactiveData.formFields Array (per item):
ParameterTypeDescriptionSample Value
elementTypestringType of form element"textInput"
elementIdstringUnique element identifier"field1"
labelstringField label text"How was your experience?"
optionalbooleanWhether field is optionalfalse

interactiveData.submitElement Object:
ParameterTypeDescriptionSample Value
elementTypestringType of element"button"
elementIdstringUnique element identifier"submit_btn"
buttonTextstringButton display text"Submit"

interactionGoal Object:
ParameterTypeDescriptionSample Value
elementIdsarrayTarget element IDs for goal[]
typestringGoal type"anyAction"

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-6"
namestringUser’s display name"Ronald Jerry"
avatarstringURL to user’s avatar"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-6.webp"
statusstringUser’s online status"online"
rolestringUser’s role"default"
lastActiveAtnumberLast active timestamp1771998694
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-7"
namestringUser’s display name"Henry Marino"
avatarstringURL to user’s avatar"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-7.webp"
statusstringUser’s online status"online"
rolestringUser’s role"default"
lastActiveAtnumberLast active timestamp1771998700
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

data Object:
ParameterTypeDescriptionSample Value
interactiveDataobjectInteractive element dataSee below ↓
interactionGoalobjectGoal configurationSee below ↓
resourcestringSDK resource identifier"REACT_NATIVE-4_0_14-..."
allowSenderInteractionbooleanWhether sender can interactfalse
entitiesobjectSender and receiver entitiesSee below ↓

data.interactiveData Object:
ParameterTypeDescriptionSample Value
titlestringForm title"Quick Survey"
formFieldsarrayForm field definitions[{"elementType": "textInput", "elementId": "field1", "label": "How was your experience?", "optional": false}]
submitElementobjectSubmit button configuration{"elementType": "button", "elementId": "submit_btn", "buttonText": "Submit"}
interactableElementIdsarrayIDs of interactable elements["submit_btn"]

data.interactionGoal Object:
ParameterTypeDescriptionSample Value
elementIdsarrayTarget element IDs for goal[]
typestringGoal type"anyAction"

data.entities Object:
ParameterTypeDescriptionSample Value
senderobjectSender entity wrapperSee below ↓
receiverobjectReceiver entity wrapperSee below ↓

data.entities.sender Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.sender.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-6"
namestringUser’s display name"Ronald Jerry"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-6.webp"
statusstringUser’s online status"online"
rolestringUser’s role"default"
lastActiveAtnumberLast active timestamp1771998694
tagsarrayUser tags[]

data.entities.receiver Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.receiver.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-7"
namestringUser’s display name"Henry Marino"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-7.webp"
statusstringUser’s online status"online"
rolestringUser’s role"default"
lastActiveAtnumberLast active timestamp1771998700
conversationIdstringConversation identifier"cometchat-uid-6_user_cometchat-uid-7"
tagsarrayUser tags[]

Event Listeners

CometChat SDK provides event listeners to handle real-time events related to InteractiveMessage.

On InteractiveMessage Received

The onInteractiveMessageReceived event listener is triggered when an InteractiveMessage is received. Here is an example:
CometChat.addMessageListener(
  "UNIQUE_LISTENER_ID",
  new CometChat.MessageListener({
    onInteractiveMessageReceived: (message) => {
      console.log("on Interactive Message Received", message);
    }
  })
);
On EventonInteractiveMessageReceived returns the received interactive message object:InteractiveMessage Object:
ParameterTypeDescriptionSample Value
idstringUnique message identifier"25308"
conversationIdstringConversation identifier"cometchat-uid-6_user_cometchat-uid-7"
receiverIdstringReceiver’s UID"cometchat-uid-7"
receiverTypestringType of receiver"user"
typestringMessage type"form"
categorystringMessage category"interactive"
sentAtnumberUnix timestamp when sent1771998776
updatedAtnumberUnix timestamp when updated1771998776
interactiveDataobjectInteractive element dataSee below ↓
interactionGoalobjectGoal for tracking interactionsSee below ↓
senderobjectSender user detailsSee below ↓
receiverobjectReceiver user detailsSee below ↓
dataobjectAdditional message dataSee below ↓
reactionsarrayMessage reactions[]
mentionedUsersarrayUsers mentioned in message[]
mentionedMebooleanWhether current user is mentionedfalse

interactiveData Object:
ParameterTypeDescriptionSample Value
titlestringForm title"Quick Survey"
formFieldsarrayForm field definitionsSee below ↓
submitElementobjectSubmit button configurationSee below ↓
interactableElementIdsarrayIDs of interactable elements["submit_btn"]

interactiveData.formFields Array (per item):
ParameterTypeDescriptionSample Value
elementTypestringType of form element"textInput"
elementIdstringUnique element identifier"field1"
labelstringField label text"How was your experience?"
optionalbooleanWhether field is optionalfalse

interactiveData.submitElement Object:
ParameterTypeDescriptionSample Value
elementTypestringType of element"button"
elementIdstringUnique element identifier"submit_btn"
buttonTextstringButton display text"Submit"

interactionGoal Object:
ParameterTypeDescriptionSample Value
elementIdsarrayTarget element IDs for goal[]
typestringGoal type"anyAction"

sender Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-6"
namestringUser’s display name"Ronald Jerry"
avatarstringURL to user’s avatar"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-6.webp"
statusstringUser’s online status"online"
rolestringUser’s role"default"
lastActiveAtnumberLast active timestamp1771998694
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-7"
namestringUser’s display name"Henry Marino"
avatarstringURL to user’s avatar"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-7.webp"
statusstringUser’s online status"online"
rolestringUser’s role"default"
lastActiveAtnumberLast active timestamp1771998700
hasBlockedMebooleanWhether user has blocked current userfalse
blockedByMebooleanWhether current user blocked this userfalse
deactivatedAtnumberDeactivation timestamp (0 if active)0
tagsarrayUser tags[]

data Object:
ParameterTypeDescriptionSample Value
interactiveDataobjectInteractive element dataSee below ↓
interactionGoalobjectGoal configurationSee below ↓
resourcestringSDK resource identifier"REACT_NATIVE-4_0_14-..."
allowSenderInteractionbooleanWhether sender can interactfalse
entitiesobjectSender and receiver entitiesSee below ↓

data.interactiveData Object:
ParameterTypeDescriptionSample Value
titlestringForm title"Quick Survey"
formFieldsarrayForm field definitions[{"elementType": "textInput", "elementId": "field1", "label": "How was your experience?", "optional": false}]
submitElementobjectSubmit button configuration{"elementType": "button", "elementId": "submit_btn", "buttonText": "Submit"}
interactableElementIdsarrayIDs of interactable elements["submit_btn"]

data.interactionGoal Object:
ParameterTypeDescriptionSample Value
elementIdsarrayTarget element IDs for goal[]
typestringGoal type"anyAction"

data.entities Object:
ParameterTypeDescriptionSample Value
senderobjectSender entity wrapperSee below ↓
receiverobjectReceiver entity wrapperSee below ↓

data.entities.sender Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.sender.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-6"
namestringUser’s display name"Ronald Jerry"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-6.webp"
statusstringUser’s online status"online"
rolestringUser’s role"default"
lastActiveAtnumberLast active timestamp1771998694
tagsarrayUser tags[]

data.entities.receiver Object:
ParameterTypeDescriptionSample Value
entityTypestringType of entity"user"
entityobjectUser entity detailsSee below ↓

data.entities.receiver.entity Object:
ParameterTypeDescriptionSample Value
uidstringUser’s unique identifier"cometchat-uid-7"
namestringUser’s display name"Henry Marino"
avatarstringUser’s avatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-7.webp"
statusstringUser’s online status"online"
rolestringUser’s role"default"
lastActiveAtnumberLast active timestamp1771998700
conversationIdstringConversation identifier"cometchat-uid-6_user_cometchat-uid-7"
tagsarrayUser tags[]

On Interaction Goal Completed

The onInteractionGoalCompleted event listener is invoked when an interaction goal is achieved. Here is an example:
CometChat.addMessageListener(
  "UNIQUE_LISTENER_ID",
  new CometChat.MessageListener({
    onInteractionGoalCompleted: (message) => {
      console.log("on Interaction Goal Completed", message);
    }
  })
);
These event listeners offer your application a way to provide real-time updates in response to incoming interactive messages and goal completions, contributing to a more dynamic and responsive user chat experience.
Listener Cleanup: Always remove message listeners when they are no longer needed. Remove the listener in componentWillUnmount() or the cleanup function of a useEffect hook to prevent memory leaks.
CometChat.removeMessageListener("UNIQUE_LISTENER_ID");

Usage

An InteractiveMessage is constructed with the receiver’s UID, the receiver type, the interactive type, and interactive data as a JSONObject. Once created, the InteractiveMessage can be sent using CometChat’s sendInteractiveMessage() method. Incoming InteractiveMessages can be received and processed via CometChat’s message listener framework.
  • Use descriptive, unique listener IDs (e.g., "interactive-form-listener") to avoid conflicts with other listeners
  • Always remove message listeners on component unmount to prevent memory leaks
  • Set allowSenderInteraction to true only when the sender needs to interact with their own message (e.g., previewing a form)
  • Define clear InteractionGoal types to accurately track user engagement with interactive elements
  • Handle both onInteractiveMessageReceived and onInteractionGoalCompleted events to provide a complete interactive experience
  • Interactive message not sending: Verify that interactiveData is a valid JSON object and all required fields (receiverId, receiverType, messageType) are set
  • Not receiving interactive messages: Ensure the message listener is registered with onInteractiveMessageReceived and the user is logged in
  • Goal not completing: Check that the InteractionGoal type matches the expected interactions — use ANY_ACTION for simple cases and ALL_OF only when every specified element must be interacted with
  • markAsInteracted failing: Confirm that the message ID and element ID are valid and that the user has permission to interact with the message
  • Duplicate events: Verify you are not registering the same listener ID multiple times without removing the previous one

Next Steps