Skip to main content
Quick Reference for AI Agents & Developers
  • Send text: CometChat.sendTextMessage(message:onSuccess:onError:)
  • Send media: CometChat.sendMediaMessage(message:onSuccess:onError:)
  • Receive messages: CometChat.addMessageListener("UNIQUE_ID", self)
  • Fetch history: MessagesRequest.MessageRequestBuilder().build()messagesRequest.fetchPrevious(onSuccess:onError:)
  • Message types: TextMessage, MediaMessage, CustomMessage, InteractiveMessage (all extend BaseMessage)
  • Related: Send Message · Receive Message · Typing Indicators
Messaging is one of the core features of CometChat. We’ve thoughtfully created methods to help you send, receive and fetch message history. At the minimum, you must add code for sending messages and receiving messages. Once you’ve implemented that, you can proceed to more advanced features like typing indicators and delivery & read receipts.

Message Data Models

All message types in CometChat inherit from the BaseMessage class. Understanding these data models is essential for working with messages.

BaseMessage Properties

The BaseMessage class contains properties common to all message types:
PropertyTypeDescription
idIntServer-assigned unique message identifier
muidStringClient-generated unique identifier
senderUidStringUID of the message sender
receiverUidStringUID of the receiver (user or group)
messageTypeMessageTypeType: .text, .image, .video, .audio, .file, .custom
receiverTypeReceiverType.user or .group
sentAtIntUnix timestamp (seconds) when sent
deliveredAtDoubleUnix timestamp when delivered
readAtDoubleUnix timestamp when read
deliveredToMeAtDoubleWhen delivered to current user
readByMeAtDoubleWhen read by current user
senderUser?User object of the sender
receiverAppEntity?User or Group object
metaData[String: Any]?Custom metadata dictionary
conversationIdStringUnique conversation identifier
parentMessageIdIntParent message ID (for threads)
replyCountIntNumber of replies
mentionedUsers[User]Array of mentioned users
mentionedMeBoolWhether current user is mentioned
reactions[ReactionCount]Reaction counts
editedAtDoubleWhen edited
deletedAtDoubleWhen deleted

TextMessage

Extends BaseMessage with:
PropertyTypeDescription
textStringThe text content
tags[String]?Message tags

MediaMessage

Extends BaseMessage with:
PropertyTypeDescription
attachmentAttachment?Single attachment
attachments[Attachment]?Multiple attachments
captionString?Caption text
tags[String]?Message tags

CustomMessage

Extends BaseMessage with:
PropertyTypeDescription
customData[String: Any]?Custom JSON data
typeString?Custom type identifier
subTypeString?Custom subtype

Attachment

PropertyTypeDescription
fileNameStringFile name
fileExtensionStringFile extension
fileSizeDoubleSize in bytes
fileMimeTypeStringMIME type
fileUrlStringURL of the file

Success & Failure Responses

Send Message Success

When a message is sent successfully, you receive the complete message object with server-assigned properties:
CometChat.sendTextMessage(message: textMessage, onSuccess: { (sentMessage) in
    // sentMessage.id - Server-assigned message ID
    // sentMessage.sentAt - Server timestamp
    // sentMessage.sender - Populated User object
    // sentMessage.conversationId - Conversation identifier
    print("Message ID: \(sentMessage.id)")
    print("Sent at: \(sentMessage.sentAt)")
}, onError: { (error) in
    print("Error: \(error?.errorDescription ?? "")")
})

Common Error Codes

Error CodeDescription
ERR_NOT_LOGGED_INUser is not logged in
ERR_UID_NOT_FOUNDReceiver UID not found
ERR_GUID_NOT_FOUNDGroup GUID not found
ERR_BLOCKED_BY_RECEIVERBlocked by receiver
ERR_NOT_A_MEMBERNot a member of the group