Skip to main content
Quick Reference for AI Agents & Developers
  • Set delegate: CometChat.messagedelegate = self (conform to CometChatMessageDelegate)
  • Protocol: Implement CometChatMessageDelegate in your view controller
  • Delegate methods: onTextMessageReceived(_:), onMediaMessageReceived(_:), onCustomMessageReceived(_:)
  • Missed messages: Use MessagesRequest with setMessageId() to fetch messages received while offline
  • Related: Send Message · Typing Indicators · Messaging Overview
Receiving messages with CometChat has two parts:
  1. Adding a listener to receive real-time messages when your app is running
  2. Calling a method to retrieve missed messages when your app was not running

Object Structures

For complete object structure definitions, see Send Message - Object Structures:

Real-time Messages

In other words, as a recipient, how do I receive messages when my app is running? In order to receive incoming messages, you must add protocol conformance CometChatMessageDelegate as Shown Below :
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())
  }
}
Do not forget to set your view controller as a CometChat delegate probably in viewDidLoad() as CometChat.messagedelegate = self
Delegate Method: CometChatMessageDelegate.onTextMessageReceived(textMessage:)
ParameterTypeValue
N/A - This is a delegate callback, not a request
The delegate method fires when a TextMessage is received in real-time.
Delegate Method: CometChatMessageDelegate.onMediaMessageReceived(mediaMessage:)
ParameterTypeValue
N/A - This is a delegate callback, not a request
The delegate method fires when a MediaMessage is received in real-time.
Delegate Method: CometChatMessageDelegate.onCustomMessageReceived(customMessage:)
ParameterTypeValue
N/A - This is a delegate callback, not a request
The delegate method fires when a CustomMessage is received in real-time.
As a sender, you will not receive your own message in a real-time message event. However, if a user is logged-in using multiple devices, they will receive an event for their own message in other devices.

Missed Messages

In other words, as a recipient, how do I receive messages that I missed when my app was not running? For most cases, you will need to add functionality to load missed messages. If you’re building an on-demand or live streaming app, you may choose to skip this and fetch message history instead. Using the same MessagesRequest class and the filters provided by the MessagesRequestBuilder class, you can fetch the message that we sent to the logged-in user but were not delivered to him as he was offline. For this, you will require the Id of the last message received. You can either maintain it at your end or use the getLastDeliveredMessageId() method provided by the CometChat class. This Id needs to be passed to the setMessageId() method of the builder class. Now using the fetchNext() method, you can fetch all the messages that were sent to the user when he/she was offline. Calling the fetchNext() method on the same object repeatedly allows you to fetch all the offline messages for the logged in user in a paginated manner

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);
}
Method: MessagesRequest.fetchNext()
ParameterTypeValue
messageIDInt37797 (last delivered message ID)
uidString"cometchat-uid-2"
limitInt50

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);
}
Method: MessagesRequest.fetchNext()
ParameterTypeValue
messageIDInt37795 (last delivered message ID)
guidString"cometchat-guid-1"
limitInt50

Unread Messages

In other words, as a logged-in user, how do I fetch the messages I’ve not read? Using the MessagesRequest class described above, you can fetch the unread messages for the logged-in user. In order to achieve this, you need to set the unread variable in the builder to true using the setUnread() method so that only the unread messages are fetched.

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

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);
}
Method: MessagesRequest.fetchPrevious()
ParameterTypeValue
uidString"cometchat-uid-2"
unreadBooltrue
limitInt30

Message History

In other words, as a logged-in user, how do I fetch the complete message history?

Fetch Message History of a particular one-on-one conversation

Using the MessagesRequest class and the filters for the MessagesRequestBuilder class as shown in the below code snippet, you can fetch the entire conversation between the logged in user and any particular user.
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);
}
Method: MessagesRequest.fetchPrevious()
ParameterTypeValue
uidString"cometchat-uid-2"
limitInt30
BaseMessageThe list of messages received is in the form of objects of BaseMessage class. A BaseMessage can either be a TextMessage or a MediaMessage. You can use the instanceOf operator to check the type of object.

Common Error Codes

Error CodeDescriptionResolution
ERROR_USER_NOT_LOGGED_INUser is not logged inCall CometChat.login() first
ERR_SDK_NOT_INITIALIZEDSDK not initializedCall CometChat.init() first
ERR_NETWORK_ERRORNetwork connectivity issueCheck internet connection and retry