> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-013b37f0.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Edit A Message

While [editing a message](/sdk/ios/2.0/edit-a-message) is straightforward, receiving events for edited messages with CometChat has two parts:

1. Adding a listener to receive [real-time message edits](/sdk/ios/2.0/edit-a-message#real-time-message-edit-events) when your app is running
2. Calling a method to retrieve [missed message edits](/sdk/ios/2.0/edit-a-message#missed-message-edit-events) 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.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let textMessage = TextMessage(receiverUid: receiverID, text: text, receiverType: .user)
    textMessage.id = messageID

    CometChat.edit(message: textMessage, onSuccess: { (baseMessage) in
                    
       print("Message edited successfully. \(baseMessage)")
                    
    }) { (error) in
                    
       print("Message edit failed with error: \(error.errorDescription)")
    }
    ```
  </Tab>
</Tabs>

The object of the edited message will be returned in the `onSucess()` callback method of the listener. The message object will contain the `editedAt` fieldset 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.

| User Role       | Conversation Type       | Edit Capabilities         |
| --------------- | ----------------------- | ------------------------- |
| Message Sender  | One-on-one Conversation | Messages they've sent     |
| Message Sender  | Group Conversation      | Messages they've sent     |
| Group Owner     | Group Conversation      | All messages in the group |
| Group Moderator | Group Conversation      | All messages in the group |

## Real-time Message Edit Events

In order to receive real-time events for the message being edited, you must add protocol conformance `CometChatMessageDelegate` as Shown Below :

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    extension ViewController: CometChatMessageDelegate {

    		func onMessageEdited(message: BaseMessage) {
            
            print("received edited message successfully.")
        }
    }
    ```
  </Tab>
</Tabs>

## 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 and `Action` message informing you that the message with id 50 has been edited.

For the message edited event, in the `Action` object received, the following fields can help you get the relevant information-

1. `action` - `edited`
2. `actionOn` - Updated message object with the edited details.
3. `actionBy` - User object containing the details of the user who has edited the message.
4. `actionFor` - User/group object having the details of the receiver to which the message was sent.

<Note>
  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.
</Note>
