Skip to main content
Quick Reference for AI Agents & Developers
  • Edit message: CometChat.edit(message:onSuccess:onError:)
  • Listen for edits: onMessageEdited(_:) in message listener delegate
  • Missed edits: Use MessagesRequest with appropriate filters
  • Related: Delete Message · Send Message · Messaging Overview
While editing a message is straightforward, receiving events for edited messages with CometChat has two parts:
  1. Adding a listener to receive real-time message edits when your app is running
  2. 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? In order to edit a message, you can use the editMessage() 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.
let textMessage = TextMessage(receiverUid: "cometchat-uid-2", text: "Updated message", receiverType: .user)
textMessage.id = 12345  // ID of message to edit

CometChat.edit(message: textMessage, onSuccess: { (message) in
    print("Message edited: \(message)")
}, onError: { (error) in
    print("Error: \(error.errorDescription)")
})
The object of the edited message will be returned in the 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.
Method: CometChat.edit(message:)Object Type: TextMessage (before editing)
ParameterTypeValue
idInt12345
receiverUidString"cometchat-uid-2"
textString"Updated message"
receiverTypeCometChat.ReceiverType0 (.user)

Add/Update Tags

While editing a message, you can update the tags associated with the Message. You can use the tags property to do so. The tags added while editing a message will replace the tags set when the message was sent.
let tags = ["pinned", "important"]
let textMessage = TextMessage(receiverUid: "cometchat-uid-2", text: "Pinned message", receiverType: .user)
textMessage.id = 12345
textMessage.tags = tags

CometChat.edit(message: textMessage, onSuccess: {...}, onError: {...})
Tags added while editing REPLACE existing tags on the message.
Method: CometChat.edit(message:)
ParameterTypeValue
idInt12345
textString"Pinned message"
tags[String]["pinned", "important"]
By default, CometChat allows certain roles to edit a message.
User RoleConversation TypeEdit Capabilities
Message SenderOne-on-oneOwn messages only
Message SenderGroupOwn messages only
Group OwnerGroupAll messages
Group ModeratorGroupAll messages

Real-time Message Edit Events

In other words, as a recipient, how do I know when someone edits their message in real-time? To receive real-time edit events, implement CometChatMessageDelegate:
extension YourViewController: CometChatMessageDelegate {
    
    func onMessageEdited(message: BaseMessage) {
        print("Message edited: \(message.id)")
        print("Edited at: \(message.editedAt)")
        print("Edited by: \(message.editedBy ?? "")")
        
        if let textMessage = message as? TextMessage {
            print("New text: \(textMessage.text)")
        }
    }
}

// Register the delegate:
CometChat.messagedelegate = self
Method: onMessageEdited(message:)Object Type: BaseMessage
ParameterTypeDescription
messageBaseMessageThe edited message object
message.idIntMessage ID
message.editedAtDoubleUnix timestamp when edited
message.editedByString?UID of user who edited
message.textStringNew text (for TextMessage)
message.customData[String: Any]?New data (for CustomMessage)

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, the editedAt and the editedBy fields will be set. Also, for example, the total number of messages for a conversation is 100, and the message with message ID 50 was edited. Now the message with id 50 will have the editedAt and the editedBy fields set whenever it is pulled from the history. Also, the 101st message will be an Action message informing you that the message with id 50 has been edited. When your app was not running, edited messages appear in two ways:
  1. Edited Message in History: The message object has editedAt and editedBy fields set
  2. Action Message: An ActionMessage is added to history indicating the edit
// Detect edited messages in history
for message in messages {
    if message.editedAt > 0 {
        print("Message \(message.id) was edited at \(message.editedAt)")
        print("Edited by: \(message.editedBy ?? "unknown")")
    }
    
    if let actionMessage = message as? ActionMessage {
        if actionMessage.action == .messageEdited {
            print("Edit action detected")
            if let editedMsg = actionMessage.actionOn as? BaseMessage {
                print("Edited message ID: \(editedMsg.id)")
            }
        }
    }
}
Object Type: ActionMessage
ParameterTypeDescription
actionString"edited"
actionOnBaseMessageUpdated message with edited details
actionByUserUser who edited the message
actionForAppEntityReceiver (User or Group)
In order 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.

Success & Failure Responses

Edit Message Success Response

When edit() is successful, the onSuccess callback returns the edited BaseMessage object:
CometChat.edit(message: textMessage, onSuccess: { (message) in
    // message: BaseMessage - The edited message object
    print("Message ID: \(message.id)")                 // Int - Message ID
    print("Edited At: \(message.editedAt)")            // Double - Edit timestamp
    print("Edited By: \(message.editedBy ?? "")")      // String? - UID of user who edited
    print("Sender UID: \(message.senderUid)")          // String - Original sender
    print("Sent At: \(message.sentAt)")                // Int - Original send timestamp
    
    // Get updated text
    if let textMessage = message as? TextMessage {
        print("Updated Text: \(textMessage.text)")     // String - New message text
    }
}, onError: { (error) in
    // Handle error
})

Edit Message Failure Response

When edit() fails, the onError callback returns a CometChatException:
CometChat.edit(message: textMessage, onSuccess: { (message) in
    // Success
}, onError: { (error) in
    print("Error Code: \(error.errorCode)")
    print("Error Description: \(error.errorDescription)")
    
    // Handle specific errors
    switch error.errorCode {
    case "ERR_NOT_LOGGED_IN":
        // User is not logged in
        break
    case "ERR_MESSAGE_NOT_FOUND":
        // Message with specified ID does not exist
        break
    case "ERR_PERMISSION_DENIED":
        // User doesn't have permission to edit this message
        break
    case "ERR_INVALID_MESSAGE_TYPE":
        // Cannot edit this message type
        break
    default:
        break
    }
})

Edited Message Properties

When a message is edited, these properties are set:
PropertyTypeDescription
editedAtDoubleUnix timestamp when message was edited
editedByString?UID of user who edited the message
updatedAtDoubleSame as editedAt for edited messages

Common Error Codes

Error CodeDescriptionResolution
ERR_NOT_LOGGED_INUser is not logged inLogin first using CometChat.login()
ERR_MESSAGE_NOT_FOUNDMessage does not existVerify the message ID is correct
ERR_PERMISSION_DENIEDNo permission to editOnly sender or group admin/moderator can edit
ERR_INVALID_MESSAGE_TYPECannot edit this typeOnly TextMessage and CustomMessage can be edited