AI Integration Quick Reference
AI Integration Quick Reference
Everything on this page is a no-op unless offline support is active for the logged-in user (
CometChat.isOfflineEnabled() is true). See Enable Offline Support. The message catch-up depends on the server’s updatedAt message changelog; against a server without it, each sweep returns nothing and missed edits, deletes, and reactions are not synced.How catch-up works
A sync runs automatically when the connection settles after a reconnect (the connection must hold for about 2.5 seconds). It never runs on a brief connect-then-drop flap, and overlapping triggers coalesce: at most one follow-up sweep is queued behind the running one. The outbox is flushed first, and the sweep starts after the flush finishes or after 20 seconds, whichever comes first. The sweep proceeds in two phases, in order:- Conversation phase — the conversation list is fetched as one fresh page. A conversation-list UI can blind-replace its list with the result.
- Message phase — the SDK replays the message changelog from the last sync point. New messages and updates to already-cached messages are merged into the cache.
DISCARDED). A refresh that fails, or whose response cannot be read, removes nothing. A single sweep request that gets no answer is aborted after 60 seconds and counts as a failed phase. A first-ever sync (cold start) seeds its starting point from the newest conversation and fetches no message history — older history is reached by scrolling online.
Listen for sync progress
Register aSyncListener to observe the lifecycle of every automatic and manual sync. Each phase fires started, then exactly one of completed / error. Every callback is optional.
- TypeScript
- JavaScript
The two phases are independent — a conversation-phase error does not stop the message phase. A phase error is informational: the cache is never left in a wrong state and the sweep resumes automatically.
The
SyncListener is self-sufficient: onMessageSyncCompleted carries the swept messages, including action and call messages that the MessageListener does not deliver, so you can use it without a MessageListener.
onMessageSyncCompleted reports the outcome:
Remove the listener with
CometChat.removeSyncListener("SYNC_LISTENER").
Receiving synced messages
If you already use aCometChat.MessageListener, synced messages still arrive there, so you usually do not need a SyncListener to keep your chat up to date:
- Genuinely new messages arrive one-by-one on
onTextMessageReceived(and the other typed variants), withgetSource()equal toMessageSource.SYNC. - Updates to already-cached messages arrive together on
onMessagesSynced(messages). Upsert the batch by id; never append it.
- TypeScript
- JavaScript
On reconnect, queued messages are sent first, then the sync runs.
forceSyncNow runs only the sync; it does not send queued messages (use retryMessage or retryAllFailedMessages for those).Check sync state
Poll the current sync state and the time of the last completed sync at any time.- TypeScript
- JavaScript
getSyncState() returns SyncState.SYNCING while a sweep is in flight, otherwise SyncState.IDLE (also when offline support is disabled). getLastSyncTimestamp() returns a server-provided epoch value in seconds, never the device clock, and 0 if the cache has never synced.
Trigger a manual sync
CallforceSyncNow to run a catch-up immediately — for example behind a pull-to-refresh. It routes through the same throttle as the automatic sync, so it will not start a second overlapping sweep or hammer the backend; manual syncs must be at least 10 seconds apart.
The returned Promise resolves when the sweep is accepted, not when it completes. The sync’s actual progress and outcome arrive on your registered SyncListener. You can instead pass an optional { onSuccess, onError } callback object.
- TypeScript
- JavaScript
When the cache resets
In rare cases the SDK cannot keep the existing cache — for example the encryption key became unrecoverable, or the database file is corrupt. When this happens it rebuilds the cache from scratch and notifies anOfflineCacheResetListener. Any un-sent messages that were queued are lost in the rebuild; pendingMessagesLost reports how many, or CometChat.OFFLINE_PENDING_COUNT_UNKNOWN (-1) when they could not be counted — that value means “unknown”, not “none”.
- TypeScript
- JavaScript
On a relaunch with a saved login, the cache can be reset during startup. The SDK delivers that reset just after
await CometChat.init(...) resolves. Add the cache-reset listener, and the outbox listener, straight after init resolves, before you await anything else, or you will miss it.
- TypeScript
- JavaScript
CometChat.removeOfflineCacheResetListener("RESET_LISTENER"). See Encrypt the Cache for the encryption-related reasons.
Manage the cache
Read the current cache size, or clear the cache entirely.- TypeScript
- JavaScript
getCacheSize() returns the total cache size in bytes (local database plus the media byte store) as a single figure, or 0 when offline support is disabled.
You never need to clear the cache on logout — the SDK does it for you. The cache is stored per user: it survives app restarts and a token refresh for the same logged-in user, and is wiped automatically on logout, when the server rejects the auth token, or when a different user or app ID is used. See Offline Support.