Use this file to discover all available pages before exploring further.
Migrating app version from v3 to v4 ?Skip the create new app step. Your existing v3 app can be migrated to v4.Follow steps mentioned in Add the CometChat dependency section below to upgrade to latest version of v4
The init() method initializes the SDK and must be called before any other CometChat method. Call it once at app startup, typically in your entry file (index.js, main.js, or App.js).
TypeScript
JavaScript
let appID: string = "APP_ID";let region: string = "APP_REGION";let appSetting: CometChat.AppSettings = new CometChat.AppSettingsBuilder().subscribePresenceForAllUsers().setRegion(region).autoEstablishSocketConnection(true).build();CometChat.init(appID, appSetting).then((initialized: boolean) => {console.log("Initialization completed successfully", initialized);},(error: CometChat.CometChatException) => {console.log("Initialization failed with error:", error);});
let appID = "APP_ID";let region = "APP_REGION";let appSetting = new CometChat.AppSettingsBuilder() .subscribePresenceForAllUsers() .setRegion(region) .autoEstablishSocketConnection(true) .build();CometChat.init(appID, appSetting).then( () => { console.log("Initialization completed successfully"); }, (error) => { console.log("Initialization failed with error:", error); });
Alternatively, you can use the async/await syntax:
let appID = "APP_ID";let region = "APP_REGION";let appSetting = new CometChat.AppSettingsBuilder() .subscribePresenceForAllUsers() .setRegion(region) .autoEstablishSocketConnection(true) .build();try { await CometChat.init(appID, appSetting); console.log("Initialization completed successfully");} catch (error) { console.log("Initialization failed with error:", error);}
Replace APP_ID and APP_REGION with your credentials from the Dashboard.
CometChat.init() must be called before any other SDK method. Calling
login(), sendMessage(), or registering listeners before init() will
fail.
The CometChat SDK requires browser APIs (window, WebSocket) and cannot run on the server. For SSR frameworks, initialize the SDK only on the client side.
Next.js
NuxtJS
Ionic/Cordova
Import the SDK dynamically in useEffect:
import React from "react";export default function Home() { let [ready, setReady] = React.useState(false); React.useEffect(() => { window.CometChat = require("@cometchat/chat-sdk-javascript").CometChat; setReady(true); }, []); return ready ? <Chat /> : <p>Loading...</p>;}