Skip to main content

Overview

CometChatUIKit is a static class that provides imperative methods for SDK initialization, authentication, and message sending. The UI Kit wraps the Chat SDK methods to manage internal eventing and keep UI components synchronized. Use these wrapper methods instead of raw SDK calls. You must call CometChatUIKit.init() and CometChatUIKit.login() (or loginWithAuthToken()) before mounting CometChatProvider. These methods are also useful for:
  • Sending messages programmatically outside the composer
  • Checking initialization/login state

Initialization

CometChatUIKit.init(settings)

Initialize the CometChat SDK and UIKit.
Returns: Promise<CometChat.User | null> — the logged-in user if a session exists, otherwise null. What it does:
  1. Initializes the CometChat SDK with app settings
  2. Sets source metadata for analytics
  3. Creates the plugin registry (default + user plugins)
  4. Initializes the localization singleton
  5. Resumes existing session (if any)
  6. Initializes Calls SDK (if enabled)

Setting Session Storage Mode

By default, session data is stored in localStorage. To use sessionStorage instead, configure it during initialization via the UIKitSettingsBuilder:
When using sessionStorage, configure it in your UIKitSettingsBuilder before calling CometChatUIKit.init():
Session storage is cleared when the browser tab is closed. Use this mode if you want users to re-authenticate on every new tab/session.

Enabling Offline Support

Offline support is off by default. To show saved chats and messages without a network and queue messages sent while offline, call setOfflineConfig() on the UIKitSettingsBuilder with enabled: true. It needs @cometchat/chat-uikit-react 7.3.0 and @cometchat/chat-sdk-javascript 4.3.1 or later; on an older SDK the setting has no effect.
The config is passed to the SDK’s AppSettingsBuilder.setOfflineConfig() unchanged; its type is CometChatOfflineConfig. CometChatUIKit.getSettings()?.getOfflineConfig() returns what you passed, not whether offline support is running; call CometChat.isOfflineEnabled() for that. See Offline Support for the options, the offline states and how each component behaves.

Setting the Layout Direction

To lay every UI Kit component out right to left (or left to right) whatever the page’s direction, call setLayoutDirection() on the UIKitSettingsBuilder. The default, "auto", follows the page’s dir. A component’s own layoutDirection prop overrides it, and so does <CometChatProvider layoutDirection> when you pass it.
init() applies the builder’s value every time, "auto" when you don’t set one. Language is a separate setting: for an Arabic chat, also pass locale="ar" to CometChatProvider. See RTL Support.

Authentication

CometChatUIKit.login(uid)

Log in a user by UID. Requires authKey in UIKitSettings.
Returns: Promise<CometChat.User>

CometChatUIKit.loginWithAuthToken(authToken)

Log in with a server-generated auth token. Preferred for production.
Returns: Promise<CometChat.User>

CometChatUIKit.logout()

Log out the current user.
Returns: Promise<void> With offline support on, logging out also deletes the user’s cached data and unsent messages from the device.

Logging out without a connection

When the server can’t be reached, logout() still ends the session on the device, then rejects. A rejection alone doesn’t mean the user is still logged in, so check the SDK before deciding where to go:

User Management

Both methods require authKey in your UIKitSettings — they throw if it is missing. Intended for development/testing; in production, create and update users server-side via the REST API.

CometChatUIKit.createUser(user)

Create a new user.
Returns: Promise<CometChat.User>

CometChatUIKit.updateUser(user)

Update an existing user.
Returns: Promise<CometChat.User>

State Getters

CometChatUIKit.getLoggedInUser()

Get the currently logged-in user (synchronous).
Returns: CometChat.User | null

CometChatUIKit.isInitialized()

Check if the SDK has been initialized.
Returns: boolean

CometChatUIKit.isCallingReady()

Check if the Calls SDK is ready. Returns: boolean

CometChatUIKit.getPluginRegistry()

Get the plugin registry instance. Returns: CometChatPluginRegistry | null

CometChatUIKit.getSettings()

Get the UIKitSettings used during initialization. Returns: UIKitSettings | null

CometChatUIKit.getConversationUpdateSettings()

Get conversation update settings fetched from the dashboard. Returns: CometChat.ConversationUpdateSettings | null

CometChatUIKit.getLayoutDirection()

Get the global layout direction set at init or with setLayoutDirection(). Returns: "auto" | "ltr" | "rtl"

Layout Direction

CometChatUIKit.setLayoutDirection(direction)

Change the global layout direction after init, for example from a settings menu.
It applies to UI Kit components mounted after the call; components already on screen keep their direction until they mount again. A component’s own layoutDirection prop still overrides it, and so does <CometChatProvider layoutDirection> while you pass it. To switch a mounted UI live, pass the direction to <CometChatProvider layoutDirection> instead. See RTL Support.

Message Sending

While a CometChatProvider is mounted, these methods publish ui:message/sent (in progress, then success or error), so a mounted CometChatMessageList and CometChatConversations show the message without any extra code. Don’t publish ui:message/sent again yourself for the same send.
With offline support on, the returned Promise resolves when the message is actually sent. A message the SDK queues while offline does not reject it. Any removal of the message before it is sent rejects the Promise with the code ERROR_OFFLINE_MESSAGE_DISCARDED: a logout, a user switch, a cache reset that lost (or may have lost) unsent messages, the user’s own Delete, or the outbox discarding it. The reason behind the removal is never the rejection’s code. See Send promises.

CometChatUIKit.sendTextMessage(message)

Send a text message. Sets muid and sentAt if not already set.
Returns: Promise<CometChat.BaseMessage>

CometChatUIKit.sendMediaMessage(message)

Send a media message (image, video, audio, file).
Returns: Promise<CometChat.BaseMessage>

CometChatUIKit.sendCustomMessage(message)

Send a custom message (polls, location, etc.).
Returns: Promise<CometChat.BaseMessage>