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.
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.
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.
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> );}
Build a client-side call component that handles joining sessions, media controls, and cleanup. The component uses the SDK instance from the context provider: