Skip to main content
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.

Receiving Card Messages

onCardMessageReceived(cardMessage:) is triggered when a card message (category "card") is received. Card Messages are receive-only — they are created via the Platform REST API or the Dashboard Bubble Builder, not sent from the SDK (see Send a Message → Card Message). The CardMessage payload exposes:

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.

Offline support

With offline support enabled (SDK 4.2.0 or later):
  • fetchPrevious and fetchNext have overloads that answer from the on-device cache first (onCachedMessages, even when empty) and then from the server (onServerMessages). The cache answers a page only when every filter on the request can be computed on the device. A single server-only filter makes the cached page empty, with no error, while the server phase still answers in full. Offline, the fetch ends with onError, code ERROR_INTERNET_UNAVAILABLE, after the cached page.
  • After a reconnect catch-up, a registered CometChatMessageDelegate also receives onMessagesSynced(_:). It carries updates to messages that were already cached, such as reactions, edits, deletes and receipts. Messages that arrived while offline still come one at a time through onTextMessageReceived, onMediaMessageReceived and the other typed callbacks.
See Show Cached Data and Stay in Sync.

Next Steps

Delivery & Read Receipts

Track when messages are delivered and read by recipients

Typing Indicators

Show real-time typing status in conversations