Skip to main content
// Edit a text message
const textMessage = new CometChat.TextMessage(receiverID, "Updated text", receiverType);
textMessage.setId(messageId);
await CometChat.editMessage(textMessage);

// Listen for real-time edits
CometChat.addMessageListener("edits", new CometChat.MessageListener({
  onMessageEdited: (message) => console.log("Edited:", message)
}));
Editing a message is straightforward. Receiving edit events has two parts:
  1. Adding a listener for real-time edits when your app is running
  2. Fetching missed edits when your app was offline

Edit a Message

Use editMessage() with a TextMessage or CustomMessage object. Set the message ID using setId().

Add/Update Tags

Use setTags() to update tags when editing. New tags replace existing ones.
let tags = ["pinnedMessage"];

textMessage.setTags(tags);  
Once the message object is ready, call editMessage().
let receiverID = "RECEIVER_UID";
let messageText = "Hello world!";
let receiverType = CometChat.RECEIVER_TYPE.USER;
let messageId = "MESSAGE_ID_OF_THE_MESSAGE_TO_BE_EDITED";
let textMessage = new CometChat.TextMessage(receiverID, messageText, receiverType);

textMessage.setId(messageId);

CometChat.editMessage(textMessage).then(
message => {
  console.log("Message Edited", message);
}, error => {
  console.log("Message editing failed with error:", error);
}
);
The edited message object is returned with editedAt (timestamp) and editedBy (UID of editor) fields set. The editMessage() method returns a BaseMessage object (or a subclass like TextMessage). Access the response data using getter methods:
FieldGetterReturn TypeDescription
idgetId()numberUnique message ID
textgetText()stringUpdated text content (TextMessage only)
sendergetSender()UserThe user who sent the message
editedAtgetEditedAt()numberTimestamp when the message was edited
editedBygetEditedBy()stringUID of the user who edited the message
By default, CometChat allows certain roles to edit a message.
User RoleConversation TypeEdit Capabilities
Message SenderOne-on-One ConversationMessages they have sent.
Message SenderGroup ConversationMessages they have sent.
Group OwnerGroup ConversationAll messages in the group.
Group ModeratorGroup ConversationAll messages in the group.

Real-time Message Edit Events

Use onMessageEdited in MessageListener to receive real-time edit events.
let listenerID = "UNIQUE_LISTENER_ID";

CometChat.addMessageListener(
listenerID,
new CometChat.MessageListener({
  onMessageEdited: message => {
    console.log("Edited Message", message);
  }
})
);
Always remove message listeners when they’re no longer needed (e.g., on component unmount or page navigation). Failing to remove listeners can cause memory leaks and duplicate event handling.
CometChat.removeMessageListener("UNIQUE_LISTENER_ID");
The onMessageEdited callback receives a BaseMessage object with the editedAt and editedBy fields set. Access the data using getter methods:
FieldGetterReturn TypeDescription
idgetId()numberUnique message ID
sendergetSender()UserThe user who sent the message
editedAtgetEditedAt()numberTimestamp when the message was edited
editedBygetEditedBy()stringUID of the user who edited the message

Missed Message Edit Events

When fetching message history, edited messages have editedAt and editedBy fields set. Additionally, an Action message is created when a message is edited. The Action object contains:
  • actionedited
  • actionOn — Updated message object
  • actionBy — User who edited the message
  • actionFor — Receiver (User/Group)
You must be the message sender or a group admin/moderator to edit a message.

Next Steps

Delete a Message

Remove messages from conversations

Send Messages

Send text, media, and custom messages

Threaded Messages

Organize conversations with message threads

Receive Messages

Listen for incoming messages in real time