Integrate CometChat Calls SDK v5 in Next.js apps with client-side setup, initialization, call UI rendering, and routing support.
This guide walks you through integrating the CometChat Calls SDK into a Next.js application. By the end, you’ll have a working video call implementation with proper server-side rendering handling and authentication.
Your App ID, Region, and API Key from the CometChat Dashboard
A Next.js project (App Router or Pages Router)
Node.js 16+ installed
Important: Client-Side Only
The CometChat Calls SDK uses browser APIs (WebRTC, DOM) that are not available during server-side rendering. You must ensure the SDK only loads and runs on the client side. This guide shows you how to handle this properly with both the App Router and Pages Router.
Step 1: Install the SDK
Install the CometChat Calls SDK package:
npm install @cometchat/calls-sdk-javascript
Step 2: Configure Environment Variables
Add your CometChat credentials to .env.local. The NEXT_PUBLIC_ prefix makes these variables available in the browser:
Never expose your API Key in production client-side code. Use a backend service to generate auth tokens securely. The API Key approach shown here is for development and testing only.
Step 3: Create the Provider
Create a context provider that handles SDK initialization and user authentication. The "use client" directive ensures this component only runs in the browser.
Wrap your application with the provider. Since the provider is a client component, you can still use it in a server component layout:
// app/layout.tsximport { CometChatCallsProvider } from "@/providers/CometChatCallsProvider";export default function RootLayout({ children }: { children: React.ReactNode }) { // In a real app, get this from your authentication system const currentUserId = "cometchat-uid-1"; return ( <html lang="en"> <body> <CometChatCallsProvider uid={currentUserId}> {children} </CometChatCallsProvider> </body> </html> );}
Step 5: Create the Call Component
Build a client-side call component that handles joining sessions, media controls, and cleanup. The component uses the SDK instance from the context provider: