Next.js Support (experimental)

SDK


  1. Add the CometChat Pro SDK.

npm i @cometchat-pro/chat

2. Import CometChat and push it to global/window object.

Option 1
 useEffect(()=>{
    window.CometChat=require('@cometchat-pro/chat').CometChat
  });

Option 2
componentDidMount(){
    window.CometChat=require('@cometchat-pro/chat').CometChat
}

3. Use dynamic()  function  from next/dynamic and create CometChatNoSSR.

import dynamic from "next/dynamic";
const CometChatNoSSR = dynamic(
  () => import('./CometChatNoSSR'),
  { ssr: false }
); 

4. CometChatNOSSR.js file should look like this.

import { Component } from "react";
import consts from './consts';
export default class CometChatNoSSR extends Component{
    constructor(props){
        super(props);
        this.state={
          user:undefined
        }
      }
    componentDidMount(){
            let appSetting = new CometChat.AppSettingsBuilder().subscribePresenceForAllUsers().setRegion(consts.region).build();
            CometChat.init(consts.appId, appSetting).then(
              () => {            
                var UID = "UID";
                var apiKey = consts.apiKey;
                CometChat.login(UID, apiKey).then(
                  user => {
                    this.setState({user})
                  },
                  error => {
                    console.log("Login failed with exception:", {