React
npm install react@17.0.2, react-dom@17.0.2
npm install @cometchat-pro/chat@2.4.0 --save
git clone https://github.com/cometchat-pro/cometchat-pro-react-ui-kit.git
chat.js
import dynamic from "next/dynamic"; import { useEffect } from "react"; const CometChatNoSSR = dynamic( () => import('./CometChatNoSSR'), { ssr: false } ); function Chat() { useEffect(() => { window.CometChat = require('@cometchat-pro/chat').CometChat }); return ( <div><CometChatNoSSR /></div> ) } export default Chat;
consts.js
module.exports = { APP_ID: "APP_ID", REGION: "REGION", AUTH_KEY: "AUTH_KEY" }
CometChatNoSSR
import { Component } from "react"; import consts from './consts'; import { CometChatUI } from "./cometchat-pro-react-ui-kit/CometChatWorkspace/src/components/index" export default class CometChatNoSSR extends Component { constructor(props) { super(props); this.state = { user: undefined } } componentDidMount() { /** Initialize CometChat */ let appSetting = new CometChat.AppSettingsBuilder().subscribePresenceForAllUsers().setRegion(consts.REGION).build(); CometChat.init(consts.APP_ID, appSetting).then( () => { /** *Log in user */ const UID = "cometchat-uid-1"; const authKey = consts.AUTH_KEY; CometChat.login(UID, authKey).then( user => { this.setState({ user }) }, error => { console.log("Login failed with exception:", { error }); } ); }, error => { console.log("Initialization failed with error:", error); // Check the reason for error and take appropriate action. } ); } render() { /** Rendering CometChatUI component of React UIKit **/ if (this.state.user) { return ( <div style={{ width: "100vw", height: "100vh" }}><CometChatUI /></div> ); } else { return (<div>Laoding...</div>); } } }