Skip to main content

Documentation Index

Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

FieldValue
Key ClassesCometChatMessageDelegate, MessagesRequest.MessageRequestBuilder
Key MethodsonTextMessageReceived(), onMediaMessageReceived(), onCustomMessageReceived(), fetchPrevious(), fetchNext()
DelegateCometChat.messagedelegate = self
PrerequisitesSDK initialized, user logged in
Receiving messages with CometChat has two parts:
  1. Adding a real-time listener to receive messages while your app is running
  2. Fetching unread or historical messages when your app starts up or the user scrolls back

Real-Time Messages

Register a CometChatMessageDelegate to receive incoming messages as they arrive. Each callback receives the specific message subclass — TextMessage, MediaMessage, or CustomMessage.
extension ViewController: CometChatMessageDelegate {

  func onTextMessageReceived(textMessage: TextMessage) {
    print("TextMessage received successfully: " + textMessage.stringValue())
  }

  func onMediaMessageReceived(mediaMessage: MediaMessage) {
    print("MediaMessage received successfully: " + mediaMessage.stringValue())
  }

  func onCustomMessageReceived(customMessage: CustomMessage) {
    print("CustomMessage received successfully: " + customMessage.stringValue())
  }
}
Don’t forget to set your view controller as the delegate in viewDidLoad():
CometChat.messagedelegate = self
As a sender, you will not receive your own message in a real-time event. However, if a user is logged in on multiple devices, they will receive the event on the other devices.

Missed Messages

Fetch messages that were sent while the app was offline using MessagesRequest with setMessageId() set to the last delivered message ID.

Fetch Missed Messages of a particular one-on-one conversation

let limit = 50
let latestId = CometChat.getLastDeliveredMessageId()
let UID = "cometchat-uid-2"

let messagesRequest = MessagesRequest.MessageRequestBuilder()
  .set(messageID: latestId)
  .set(uid: UID)
  .set(limit: limit)
  .build()

messagesRequest.fetchNext(onSuccess: { (messages) in
  for message in messages! {
    if let receivedMessage = (message as? TextMessage) {
      print("Message received successfully. " + receivedMessage.stringValue())
    } else if let receivedMessage = (message as? MediaMessage) {
      print("Message received successfully. " + receivedMessage.stringValue())
    }
  }
}) { (error) in
  print("Message receiving failed with error: " + error!.errorDescription);
}

Fetch Missed Messages of a particular group conversation

let limit = 50
let latestId = CometChat.getLastDeliveredMessageId()
let GUID = "cometchat-guid-1"

let messagesRequest = MessagesRequest.MessageRequestBuilder()
  .set(messageID: latestId)
  .set(guid: GUID)
  .set(limit: limit)
  .build()

messagesRequest.fetchNext(onSuccess: { (messages) in
  for message in messages! {
    if let receivedMessage = (message as? TextMessage) {
      print("Message received successfully. " + receivedMessage.stringValue())
    } else if let receivedMessage = (message as? MediaMessage) {
      print("Message received successfully. " + receivedMessage.stringValue())
    }
  }
}) { (error) in
  print("Message receiving failed with error: " + error!.errorDescription);
}

Unread Messages

Fetch unread messages by adding setUnread(true) to the builder. Use fetchPrevious() to retrieve them.
let limit = 50
let UID = "cometchat-uid-2"

let messagesRequest = MessagesRequest.MessageRequestBuilder()
  .set(unread: true)
  .set(limit: limit)
  .set(uid: UID)
  .build()

messagesRequest.fetchPrevious(onSuccess: { (messages) in
  for message in messages! {
    if let receivedMessage = (message as? TextMessage) {
      print("Message received successfully. " + receivedMessage.stringValue())
    } else if let receivedMessage = (message as? MediaMessage) {
      print("Message received successfully. " + receivedMessage.stringValue())
    }
  }
}) { (error) in
  print("Message receiving failed with error: " + error!.errorDescription);
}

Message History

Fetch the full conversation history using fetchPrevious(). Call it repeatedly on the same request object to paginate.
let limit = 50
let UID = "cometchat-uid-2"

let messagesRequest = MessagesRequest.MessageRequestBuilder()
  .set(limit: limit)
  .set(uid: UID)
  .build()

messagesRequest.fetchPrevious(onSuccess: { (messages) in
  for message in messages! {
    if let receivedMessage = (message as? TextMessage) {
      print("Message received successfully. " + receivedMessage.stringValue())
    } else if let receivedMessage = (message as? MediaMessage) {
      print("Message received successfully. " + receivedMessage.stringValue())
    }
  }
}) { (error) in
  print("Message receiving failed with error: " + error!.errorDescription);
}
The list of messages received is in the form of BaseMessage objects. A BaseMessage can be a TextMessage, MediaMessage, or CustomMessage. Use type casting to check the type.

Next Steps

Delivery & Read Receipts

Track when messages are delivered and read by recipients

Typing Indicators

Show real-time typing status in conversations