AI Integration Quick Reference
AI Integration Quick Reference
Media caching applies only while offline support is active for the logged-in user (
CometChat.isOfflineEnabled() is true). See Enable Offline Support.Caching modes
The caching mode is set once, in theOfflineConfig you pass at initialization, and is fixed for the SDK session. There is no runtime switch — to change it, re-initialize the SDK with a new config.
Sending media always works in both modes.
CometChat.getMediaCachingMode(). It returns .off whenever offline support is not active, whatever the app configured, so a UI layer can rely on it without reading the config itself.
What downloads automatically
In.auto mode, media bytes are downloaded and cached automatically on the two live receive paths:
- a realtime media message received while connected, and
- opening a chat online (the server read refreshes and caches its media).
.auto and .off.
In .off mode, nothing is auto-downloaded on any path; media loads live from its URL when online.
Reading cached media
When bytes are already cached, the SDK hydrates the local path automatically on every message it delivers — you do not have to ask. Read it straight off the attachment:getCachedMediaPath with the attachment’s URL. It returns nil when the bytes are not cached (call downloadMedia to fetch them).
Each attachment of a multi-attachment message resolves independently. An attachment with no cached bytes keeps
localPath == nil and the UI loads it from the URL as before.Downloading on demand
CalldownloadMedia to fetch and cache an attachment’s bytes — for example when the user taps a placeholder. Pass the attachment’s fileUrl and the message’s server id. The SDK resolves the bytes in order: the cache, then the URL, then, if the server rejects the URL as expired or forbidden (HTTP 401, 403 or 410), one re-signed URL (looked up with the message id).
downloadMedia(url:messageServerId:onSuccess:onError:).
onProgress reports 0...100, best-effort and never going backwards. It is emitted only when the server reports a content length; for an unknown-length download it is skipped and the call goes straight to onSuccess. Progress never blocks or fails the download. Dispatch to the main queue before updating UI from these closures.
onError carries an offline error code, never a raw HTTP status:
Observe background downloads
In.auto mode, incoming media downloads in the background with no per-call callback. Register a MediaDownloadListener to learn when the bytes land (or fail) so a UI tile can refresh. Callbacks fire on the main thread.
CometChat.removeMediaDownloadListener("MEDIA_LISTENER"). The background MediaDownloadListener carries no progress callback — progress is reported only on the explicit downloadMedia path above.
Media cache size limit
Cached media bytes are bounded bymaxMediaCacheSize (default 500 MB; 0 means unlimited). When the cache exceeds the limit, the least recently accessed media is deleted first until it is back under budget. The outgoing copies of un-sent messages and in-progress uploads are never evicted and do not count toward the budget, so the cache can briefly exceed maxMediaCacheSize. Bulk list loads and syncs do not refresh an item’s recency. An outgoing media send that cannot fit the budget fails with ERROR_OFFLINE_MEDIA_BYTE_OVERFLOW (see Send While Offline).
Media is stored as files in durable, app-private storage; the location is not configurable. Eviction runs silently in the background — there is no cache-limit callback. To reason about size, use CometChat.getCacheSize() and the configured limits. See the configuration reference for the full set of options.