Quick Reference - Edit a text message:
- Adding a listener to receive real-time message edits when your app is running.
- Calling a method to retrieve missed message edits when your app was not running.
Edit a Message
In other words, as a sender, how do I edit a message? To edit a message, you can use theeditMessage() method. This method takes an object of the BaseMessage class. At the moment, you are only allowed to edit TextMessage and CustomMessage. Thus, the BaseMessage object must either be a Text or a Custom Message.
Add/Update Tags
While editing a message, you can update the tags associated with the message. Use thesetTags() method to do so. The tags added while editing a message will replace the tags set when the message was sent.
- JavaScript (Text)
- JavaScript (Custom)
- TypeScript (Text)
- TypeScript (Custom)
editMessage() method and pass the message object to it.
- JavaScript
- TypeScript
Response
Response
On Success —
editMessage() returns the edited message object with editedAt and editedBy fields:Message Object:sender Object:receiver Object:data Object:data.entities Object:data.entities.sender Object:data.entities.sender.entity Object:data.entities.receiver Object:data.entities.receiver.entity Object:data.metadata Object:metadata Object:onSuccess() callback method of the listener. The message object will contain the editedAt field set with the timestamp of the time the message was edited. This will help you identify if the message was edited while iterating through the list of messages. The editedBy field is also set to the UID of the user who edited the message.
By default, CometChat allows certain roles to edit a message.
Real-time Message Edit Events
In other words, as a recipient, how do I know when someone has edited their message when my app is running? To receive real-time events for a message being edited, you need to override theonMessageEdited() method of the MessageListener class.
- JavaScript
- TypeScript
Response
Response
On Event —
onMessageEdited callback receives the edited message object:Message Object:sender Object:receiver Object:data Object:data.entities Object:data.entities.sender Object:data.entities.sender.entity Object:data.entities.receiver Object:data.entities.receiver.entity Object:data.metadata Object:metadata Object:Missed Message Edit Events
In other words, as a recipient, how do I know when someone edited their message when my app was not running? When you retrieve the list of previous messages, for the message that was edited, theeditedAt and the editedBy fields will be set. Also, for example, if the total number of messages for a conversation is 100, and the message with message ID 50 was edited, the message with ID 50 will have the editedAt and the editedBy fields set whenever it is pulled from the history. Additionally, the 101st message will be an [Action] message informing you that the message with ID 50 has been edited.
Response
Response
When fetching message history — edited messages have
editedAt and editedBy fields set:Message Object (per item in array):sender Object:receiver Object:data Object:data.entities Object:data.entities.sender Object:data.entities.sender.entity Object:data.entities.receiver Object:data.entities.receiver.entity Object:data.metadata Object:metadata Object:Action object received, the following fields can help you get the relevant information:
action-editedactionOn- Updated message object with the edited details.actionBy- User object containing the details of the user who has edited the message.actionFor- User/group object having the details of the receiver to which the message was sent.
To edit a message, you need to be either the sender of the message or the admin/moderator of the group in which the message was sent.
Best Practices
Best Practices
- Always verify the message type (
TextMessageorCustomMessage) before attempting an edit, as other message types are not supported. - Use the
editedAtandeditedByfields to display edit indicators in your UI so users know a message has been modified. - Register your message edit listener early in the component lifecycle to avoid missing real-time edit events.
- When updating tags via
setTags(), remember that the new tags fully replace any previously set tags — merge existing tags manually if you need to preserve them.
Troubleshooting
Troubleshooting
- Edit fails with permission error: Ensure the current user is the message sender, or a group owner/moderator for group messages.
onMessageEditednot firing: Confirm thatCometChat.addMessageListener()has been called with a unique listener ID and that the listener has not been removed prematurely.- Edited message not reflected in history: After editing, re-fetch or update the local message list. The
editedAtfield on the message object confirms the edit was applied. - Tags not updating: Make sure you call
setTags()on the message object before passing it toeditMessage(). Tags set during editing replace all previous tags.
Next Steps
Delete a Message
Remove messages from conversations for users or groups.
Send a Message
Send text, media, and custom messages to users and groups.
Receive Messages
Listen for incoming messages in real time and fetch missed messages.
Threaded Messages
Send and receive replies within message threads.