CometChat’s Calls feature offers advanced functionality for seamlessly integrating one-on-one and group audio/video calling into your application. This document provides a technical overview of how these features are implemented using the React Native UI Kit.
First, make sure that you’ve correctly integrated the UI Kit library into your project. If you haven’t done this yet or are facing difficulties, refer to our Getting Started guide. This guide will walk you through a step-by-step process of integrating our UI Kit into your React Native project.Once you’ve successfully integrated the UI Kit, the next step is to add the CometChat Calls SDK to your project. This is necessary to enable the calling features in the UI Kit. Here’s how you do it:
Copy
Ask AI
npm install @cometchat/calls-sdk-react-native
Once the dependency is added, the React Native UI Kit will automatically detect it and enable calling features. Your application will now support both audio and video calls and the CallButtons component will appear within the MessageHeader component.
In addition to CallButtons, the Calls UI Kit offers fully functional UI components for handling Incoming, Outgoing, and Ongoing calls. To receive call events in your desired component or screen, you must register a call listener using the addCallListener() method. The onIncomingCallReceived() event is triggered whenever an incoming call is received.
Copy
Ask AI
import React, { useEffect, useRef, useState } from "react";import { SafeAreaView } from "react-native";import { CometChat } from "@cometchat/chat-sdk-react-native";import { CometChatIncomingCall } from "@cometchat/chat-uikit-react-native";// Track whether the user is logged inconst [loggedIn, setLoggedIn] = useState(false);// Track if there is an incoming call to displayconst [callReceived, setCallReceived] = useState(false);// Store the incoming call object for use in the UIconst incomingCall = useRef<CometChat.Call | CometChat.CustomMessage | null>( null);// Unique ID for registering and removing the call listenervar listenerID: string = "UNIQUE_LISTENER_ID";const App = (): React.ReactElement => { useEffect(() => { // Register the call listener when the component mounts or when login state changes CometChat.addCallListener( listenerID, new CometChat.CallListener({ // Fired when an incoming call is received onIncomingCallReceived: (call: CometChat.Call) => { // Store the incoming call and update state. incomingCall.current = call; // Trigger UI to show incoming call screen setCallReceived(true); }, // Fired when an outgoing call is rejected by the recipient onOutgoingCallRejected: () => { // Clear the call state if outgoing call is rejected. incomingCall.current = null; // Clear the call object setCallReceived(false); // Hide any call UI }, onIncomingCallCancelled: () => { // Clear the call state if the incoming call is cancelled. incomingCall.current = null; setCallReceived(false); }, }) ); // Cleanup: remove the call listener when the component unmounts or before re-running return () => { CometChat.removeCallListener(listenerID); }; }, [loggedIn]); // Re-run effect if the login state changes return ( <SafeAreaView style={{ flex: 1 }}> {/* Render the incoming call UI when logged in and a call has been received */} {loggedIn && callReceived && incomingCall.current ? ( <CometChatIncomingCall call={incomingCall.current} onDecline={() => { // Handle call decline by clearing the incoming call state. incomingCall.current = null; // Clear the call object setCallReceived(false); // Hide the incoming call UI }} /> ) : null} </SafeAreaView> );};
The Incoming Call component of the CometChat UI Kit provides the functionality that lets users receive real-time audio and video calls in the app.When a call is made to a user, the Incoming Call component triggers and displays a call screen. This call screen typically displays the caller information and provides the user with options to either accept or reject the incoming call.
The Outgoing Call component of the CometChat UI Kit is designed to manage the outgoing call process within your application. When a user initiates an audio or video call to another user or group, this component displays an outgoing call screen, showcasing information about the recipient and the call status.Importantly, the Outgoing Call component is smartly designed to transition automatically into the ongoing call screen once the receiver accepts the call. This ensures a smooth flow from initiating the call to engaging in a conversation, without any additional steps required from the user.
Call Logs component provides you with the records call events such as who called who, the time of the call, and the duration of the call. This information can be fetched from the CometChat server and displayed in a structured format for users to view their past call activities.