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.
Prerequisites
Before you begin, ensure you have:
- A CometChat account with an app created (Sign up)
- 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:
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.
Step 4: Add Provider to Layout (App Router)
Wrap your application with the provider. Since the provider is a client component, you can still use it in a server component layout:
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:
Step 6: Create the Call Page
Create a page that uses the call component. This example shows a simple interface where users can enter a session ID and join a call:
Dynamic Route for Call Sessions
For a cleaner URL structure, create a dynamic route that accepts the session ID as a parameter:
Pages Router Setup
If you’re using the Pages Router instead of the App Router, use dynamic imports to prevent server-side rendering of the SDK:
Custom Hook (Optional)
For more complex applications, extract call logic into a reusable hook:
For more detailed information on specific topics covered in this guide, refer to the main documentation: