> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Setup

> Install, import, and initialize the CometChat JavaScript SDK in your web app.

> [!NOTE]
>
> 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

## Get your Application Keys

[Signup for CometChat](https://app.cometchat.com) and then:

1. Create a new app
2. Head over to the **API & Auth Keys** section and note the **Auth Key**, **App ID** & **Region**

## Add the CometChat Dependency

### NPM

> [tab] JavaScript
>
> ```js theme={null}
> npm install @cometchat/chat-sdk-javascript
> ```
>
> Then import wherever needed:
>
> ```javascript theme={null}
> import { CometChat } from "@cometchat/chat-sdk-javascript";
> ```
>
> ### CDN
>
> ```html theme={null}
> <script
>   type="text/javascript"
>   src="https://unpkg.com/@cometchat/chat-sdk-javascript/CometChat.js"
> ></script>
> ```
>
> When using the CDN, `CometChat` is available as a global variable.

## Initialization

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`).

> [tab] TypeScript
>
> ```typescript theme={null}
> 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);
> }
> );
>
> ```

> [tab] JavaScript
>
> ```javascript theme={null}
> 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:
>
> ```javascript theme={null}
> 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](https://app.cometchat.com).

> [!WARNING]
>
> `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`) | 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 | `true` |
| `overrideAdminHost(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):

```javascript theme={null}
// 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](https://www.cometchat.com/docs/sdk/javascript/user-presence.md) for more details.

### WebSocket Connection

By default, the SDK manages WebSocket connections automatically. To manage them manually:

```javascript theme={null}
let appSetting = new CometChat.AppSettingsBuilder()
  .setRegion(region)
  .autoEstablishSocketConnection(false)
  .build();
```

See [Managing WebSocket Connections](https://www.cometchat.com/docs/sdk/javascript/managing-web-sockets-connections-manually.md) for manual control.

### Session Storage

Use session storage instead of local storage (data clears when browser closes):

```javascript theme={null}
let appSetting = new CometChat.AppSettingsBuilder()
  .setRegion(region)
  .setStorageMode(CometChat.StorageMode.SESSION)
  .build();
```

## SSR Compatibility

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.

> [tab] Next.js
>
> Import the SDK dynamically in `useEffect`:
>
> ```javascript theme={null}
> 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>;
> }
> ```

> [tab] NuxtJS
>
> Import the SDK in the `mounted` lifecycle hook:
>
> ```javascript theme={null}
> export default {
>   data() {
>     return { ready: false };
>   },
>   mounted() {
>     window.CometChat = require("@cometchat/chat-sdk-javascript").CometChat;
>     this.ready = true;
>   }
> };
> ```

> [tab] Ionic/Cordova
>
> Use the JavaScript SDK directly. Import and initialize in your root component:
>
> ```typescript theme={null}
> import { CometChat } from "@cometchat/chat-sdk-javascript";
>
> ngOnInit() {
>   const appSetting = new CometChat.AppSettingsBuilder()
>     .subscribePresenceForAllUsers()
>     .setRegion("APP_REGION")
>     .autoEstablishSocketConnection(true)
>     .build();
>
>   CometChat.init("APP_ID", appSetting).then(
>     () => console.log("CometChat initialized successfully"),
>     (error) => console.log("CometChat initialization failed:", error)
>   );
> }
> ```
>
> > [!NOTE]
> >
> > The dedicated Ionic Cordova SDK has been deprecated. For new Ionic/Cordova
> > applications, use the JavaScript SDK as shown above.

***

## Next Steps

> [card] [Authentication](https://www.cometchat.com/docs/sdk/javascript/authentication-overview.md)
>
> Log in users with Auth Key or Auth Token

> [card] [Send Messages](https://www.cometchat.com/docs/sdk/javascript/send-message.md)
>
> Send your first message


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.