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).
Basic Initialization
JavaScript
TypeScript
Async/Await
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 );
}
);
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 ();
try {
await CometChat . init ( appID , appSetting );
console . log ( "Initialization completed successfully" );
} catch ( error ) {
console . log ( "Initialization failed with error:" , error );
}
Replace APP_ID with your CometChat App ID and APP_REGION with your Region from the Dashboard .
CometChat.init() must be called before any other SDK method. Calling login(), sendMessage(), or registering listeners before init() will fail.
Parameters
Parameter Type Description appID string Your CometChat App ID appSetting AppSettings Configuration object built with AppSettingsBuilder
AppSettings Options
Method Description Default setRegion(region)Region where your app was created (us, eu, in, in-private) Required subscribePresenceForAllUsers()Subscribe to presence events for all users — subscribePresenceForRoles(roles)Subscribe to presence for specific roles — subscribePresenceForFriends()Subscribe to presence for friends only — autoEstablishSocketConnection(bool)Let SDK manage WebSocket connections trueoverrideAdminHost(adminHost)Custom admin URL (dedicated deployment) — overrideClientHost(clientHost)Custom client URL (dedicated deployment) — setStorageMode(storageMode)Local storage mode (CometChat.StorageMode.SESSION for session storage) —
Presence Subscription
Choose how to subscribe to user presence (online/offline status):
// All users
new CometChat . AppSettingsBuilder ()
. subscribePresenceForAllUsers ()
. setRegion ( region )
. build ();
// Specific roles
new CometChat . AppSettingsBuilder ()
. subscribePresenceForRoles ([ "admin" , "moderator" ])
. setRegion ( region )
. build ();
// Friends only
new CometChat . AppSettingsBuilder ()
. subscribePresenceForFriends ()
. setRegion ( region )
. build ();
See User Presence for more details.
WebSocket Connection
By default, the SDK manages WebSocket connections automatically. To manage them manually:
let appSetting = new CometChat . AppSettingsBuilder ()
. setRegion ( region )
. autoEstablishSocketConnection ( false )
. build ();
See Managing WebSocket Connections for manual control.
Session Storage
Use session storage instead of local storage (data clears when browser closes):
let appSetting = new CometChat . AppSettingsBuilder ()
. setRegion ( region )
. setStorageMode ( CometChat . StorageMode . SESSION )
. build ();
Next Steps
Authentication Log in users with Auth Key or Auth Token
SSR Compatibility Setup for Next.js, Nuxt, and Ionic