Skip to main content
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.
Don’t forget to set your view controller as the delegate in viewDidLoad():
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

Fetch Missed Messages of a particular group conversation

Unread Messages

Fetch unread messages by adding setUnread(true) to the builder. Use fetchPrevious() to retrieve them.

Message History

Fetch the full conversation history using fetchPrevious(). Call it repeatedly on the same request object to paginate.
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