Skip to main content
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

Import the SDK dynamically in useEffect (functional components) or componentDidMount (class components).
import React from "react";
import Chat from "./Chat";

export default function Home() {
  let [libraryImported, setLibraryImported] = React.useState(false);

  React.useEffect(() => {
    window.CometChat = require("@cometchat/chat-sdk-javascript").CometChat;
    setLibraryImported(true);
  }, []);

  return libraryImported ? <Chat /> : <p>Loading....</p>;
}

NuxtJS

Import the SDK dynamically in the mounted lifecycle hook.
<template>
  <div v-if="libraryImported">
    <chat/>
  </div>
  <div v-else>Loading....</div>
</template>

<script>
export default {
  name: "Index",
  components: {
    'chat': () => import('./chat')
  },
  data() {
    return {
      libraryImported: false
    }
  },
  mounted() {
    window.CometChat = require('@cometchat/chat-sdk-javascript').CometChat;
    this.libraryImported = true;
  }
}
</script>

Ionic/Cordova

For Ionic and Cordova applications, use the JavaScript SDK directly. Import and initialize in your root component:
import { Component, OnInit } from '@angular/core';
import { CometChat } from '@cometchat/chat-sdk-javascript';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
})
export class AppComponent implements OnInit {
  
  ngOnInit() {
    this.initCometChat();
  }

  initCometChat() {
    const appID = 'APP_ID';
    const region = 'APP_REGION';
    
    const appSetting = new CometChat.AppSettingsBuilder()
      .subscribePresenceForAllUsers()
      .setRegion(region)
      .autoEstablishSocketConnection(true)
      .build();

    CometChat.init(appID, appSetting).then(
      () => {
        console.log('CometChat initialized successfully');
      },
      (error) => {
        console.log('CometChat initialization failed:', error);
      }
    );
  }
}
The dedicated Ionic Cordova SDK has been deprecated. For new Ionic/Cordova applications, use the JavaScript SDK as shown above. Existing users can refer to the legacy documentation.

Next Steps

Authentication

Log in users with Auth Key or Auth Token

Send Messages

Send your first message