Introduction
Participants in a multi-participant video call may find it difficult to engage in meaningful talks without interrupting each other. It is also challenging to enhance programs with real-time messaging capabilities, which facilitate communication between participants during video chats. In this environment, the goal is to create more immersive and engaging user experiences, with services such as chat emerging as essential components.
It enables users to share messages, transfer files, and swap data without disrupting the audio or video feeds. This is especially handy when participants want to ask questions, provide feedback, or provide relevant information during a video chat. This article will walk you through the process of implementing a chat feature into a React Native video call app with VideoSDK.
Getting Started with VideoSDK
Goals
By the End of this Article, we'll:
- Create a VideoSDK account and generate your VideoSDK auth token.
- Integrate the VideoSDK library and dependencies into your project.
- Implement core functionalities for video calls using VideoSDK.
- Enable the Chat feature in your app.
To take advantage of the chat functionality, we must use the capabilities that the VideoSDK offers. Before diving into the implementation steps, ensure you complete the necessary prerequisites.
Create a VideoSDK Account
Go to your VideoSDK dashboard and sign up if you don't have an account. This account gives you access to the required Video SDK token, which acts as an authentication key that allows your application to interact with VideoSDK functionality.
Generate your Auth Token
Visit your VideoSDK dashboard and navigate to the "API Key" section to generate your auth token. This token is crucial in authorizing your application to use VideoSDK features.
For a more visual understanding of the account creation and token generation process, consider referring to the provided tutorial.
Prerequisites and Setup
Make sure your development environment meets the following requirements:
- Node.js v12+
- NPM v6+ (comes installed with newer Node versions)
- Android Studio or Xcode installed
⬇️ Install VideoSDK Config.
It is necessary to set up VideoSDK within your project before going into the details of integrating the Chat feature. Installing VideoSDK using NPM or Yarn will depend on the needs of your project.
- For NPM
npm install "@videosdk.live/react-native-sdk" "@videosdk.live/react-native-incallmanager"
- For Yarn
yarn add "@videosdk.live/react-native-sdk" "@videosdk.live/react-native-incallmanager"
Project Configuration
Before integrating the Chat functionality, ensure that your project is correctly prepared to handle the integration. This setup consists of a sequence of steps for configuring rights, dependencies, and platform-specific parameters so that VideoSDK can function seamlessly inside your application context.
Android Setup
- Add the required permissions in the
AndroidManifest.xml
file.
- Update your
colors.xml
file for internal dependencies.
- Link the necessary VideoSDK Dependencies.
- Include the following line in your
proguard-rules.pro
file (optional: if you are using Proguard)
- In your
build.gradle
file, update the minimum OS/SDK version to23
.
iOS Setup
IMPORTANT: Ensure that you are using CocoaPods version 1.10 or later.
- To update CocoaPods, you can reinstall the
gem
using the following command:
$ sudo gem install cocoapods
2. Manually link react-native-incall-manager (if it is not linked automatically).
Select Your_Xcode_Project/TARGETS/BuildSettings
, in Header Search Paths, add "$(SRCROOT)/../node_modules/@videosdk.live/react-native-incall-manager/ios/RNInCallManager"
3. Change the path of react-native-webrtc
using the following command:
pod ‘react-native-webrtc’, :path => ‘../node_modules/@videosdk.live/react-native-webrtc’
4. Change the version of your platform.
You need to change the platform field in the Podfile to 12.0 or above because react-native-webrtc doesn't support iOS versions earlier than 12.0. Update the line: platform: ios, ‘12.0’.
5. Install pods.
After updating the version, you need to install the pods by running the following command:
Pod install
6. Add “libreact-native-webrtc.a” binary.
Add the "libreact-native-webrtc.a" binary to the "Link Binary With Libraries" section in the target of your main project folder.
7. Declare permissions in Info.plist:
Add the following lines to your info.plist file located at (project folder/ios/projectname/info.plist):
Register Service
Register VideoSDK services in your root index.js
file for the initialization service.
Essential Steps for Implement the Video Calling Functionality
Step 1: Get started with api.js
Before moving on, you must create an API request to generate a unique meetingId. You will need an authentication token, which you can create either through the videosdk-rtc-api-server-examples or directly from the VideoSDK Dashboard for developers.
Step 2: Wireframe App.js with all the components
To build up a wireframe of App.js, you need to use VideoSDK Hooks and Context Providers. VideoSDK provides MeetingProvider, MeetingConsumer, useMeeting, and useParticipant hooks.
First, you need to understand the Context Provider and Consumer. Context is primarily used when some data needs to be accessible by many components at different nesting levels.
- MeetingProvider: This is the Context Provider. It accepts value
config
andtoken
as props. The Provider component accepts a value prop to be passed to consuming components that are descendants of this Provider. One Provider can be connected to many consumers. Providers can be nested to override values deeper within the tree. - MeetingConsumer: This is the Context Consumer. All consumers that are descendants of a Provider will re-render whenever the Provider’s value prop changes.
- useMeeting: This is the meeting hook API. It includes all the information related to meetings such as join, leave, enable/disable the mic or webcam, etc.
- useParticipant: This is the participant hook API. It is responsible for handling all the events and props related to one particular participant such as name, webcamStream, micStream, etc.
The Meeting Context provides a way to listen for any changes that occur when a participant joins the meeting or makes modifications to their microphone, camera, and other settings.
Begin by making a few changes to the code in the App.js file.
Step 3: Implement Join Screen
The join screen will serve as a medium to either schedule a new meeting or join an existing one.
Step 4: Implement Controls
The next step is to create a ControlsContainer
component to manage features such as Join or leave a Meeting and Enable or Disable the Webcam/Mic.
In this step, the useMeeting
hook is utilized to acquire all the required methods such as join()
, leave()
, toggleWebcam
and toggleMic
.
Step 5: Render Participant List
After implementing the controls, the next step is to render the joined participants.
You can get all the joined participants
from the useMeeting
Hook.
Step 6: Handling Participant's Media
Before Handling the Participant's Media, you need to understand a couple of concepts.
1. useParticipant Hook
The useParticipant
hook is responsible for handling all the properties and events of one particular participant who joined the meeting. It will take participantId
as argument.
2. MediaStream API
The MediaStream API is beneficial for adding a MediaTrack to the RTCView
component, enabling the playback of audio or video.
Rendering Participant Media
Congratulations! By following these steps, you're on your way to unlocking the video within your application. Now, we are moving forward to integrate the feature that builds immersive video experiences for your users!
Integrate Chat Feature
For communication or any kind of messaging between participants, VideoSDK provides the usePubSub
hook, which utilizes the Publish-Subscribe mechanism. It can be employed to develop a wide variety of functionalities. For example, participants could use it to send chat messages to each other, share files or other media, or even trigger actions like muting or unmuting audio or video.
This guide focuses on using PubSub to implement Chat functionality. If you are not familiar with the PubSub mechanism and usePubSub
hook, you can follow this guide.
Implementing Chat
The initial step in setting up a group chat involves selecting a topic to which all participants will publish and subscribe, facilitating the exchange of messages. In the following example, CHAT is used as the topic. Next, obtain the publish()
method and the messages array from the usePubSub
hook.
Step 1: Add another button in ControlsContainer
to enable chat functionality and open the Chat Modal.
// MeetingView component to manage the meeting and chat functionality
function MeetingView() {
const [modalVisible, setModalVisible] = useState(false);
// Function to toggle the visibility of the modal
const toggleModal = () => {
setModalVisible(!modalVisible);
};
return (
<View style={{ flex: 1 }}>
{/* Other components for the meeting view */}
<ChatView modalVisible={modalVisible} toggleModal={toggleModal} />
<ControlsContainer
// other props, join, leave etc.
enableChat={() => {
toggleModal();
}}
/>
</View>
);
}
function ControlsContainer({ enableChat }) {
return (
// Container for control buttons
<View
style={{
padding: 24,
flexDirection: "row",
justifyContent: "space-between",
}}
>
{/* Button to enable chat */}
<Button
onPress={() => {
enableChat();
}}
buttonText={"Chat"}
backgroundColor={"#1178F8"}
/>
</View>
);
}
Step 2: Add React Native Modal component to handle chat functionality.
import {
TextInput,
Modal,
Pressable,
} from "react-native";
import {
usePubSub,
} from "@videosdk.live/react-native-sdk";
// ChatView component for displaying chat messages and input
function ChatView({ modalVisible, toggleModel }) {
// Destructure publish method from usePubSub hook
const { publish, messages } = usePubSub("CHAT");
// State to store the user typed message
const [message, setMessage] = useState("");
// Function to handle sending messages
const handleSendMessage = () => {
// Publish the message using the publish method
publish(message, { persist: true });
// Clear the message input after sending
setMessage("");
};
return (
<View
style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
marginTop: 22,
}}
>
<Modal animationType="slide" visible={modalVisible}>
<SafeAreaView
style={{
flex: 1,
backgroundColor: "#050A0E",
justifyContent: "space-between",
}}
>
<Pressable
style={{
height: 40,
aspectRatio: 1,
backgroundColor: "#5568FE",
justifyContent: "center",
alignItems: "center",
borderRadius: 24,
marginTop: 12,
marginLeft: 12,
}}
onPress={toggleModel}
>
<Text style={{ fontWeight: "bold", fontSize: 24 }}>X</Text>
</Pressable>
<View>
{/* Render chat messages */}
{messages.map((message) => {
return (
<Text
style={{
fontSize: 12,
color: "#FFFFFF",
marginVertical: 8,
marginHorizontal: 12,
}}
>
{message.senderName} says {message.message}
</Text>
);
})}
<View
style={{
paddingHorizontal: 12,
}}
>
{/* Render text input container */}
<TextInputContainer
message={message}
setMessage={setMessage}
sendMessage={handleSendMessage}
/>
</View>
</View>
</SafeAreaView>
</Modal>
</View>
);
}
Step 3: Implement the TextInputContainer component for inputting and sending messages.
// TextInputContainer component for inputting and sending messages
function TextInputContainer({ sendMessage, setMessage, message }) {
// Function to render the text input UI
const textInput = () => {
return (
<View
style={{
height: 40,
marginBottom: 14,
flexDirection: "row",
borderRadius: 10,
backgroundColor: "#404B53",
}}
>
<View
style={{
flexDirection: "row",
flex: 2,
justifyContent: "center",
alignItems: "center",
}}
>
{/* TextInput for typing messages */}
<TextInput
multiline
value={message}
placeholder={"Write your message"}
style={{
flex: 1,
color: "white",
marginLeft: 12,
margin: 4,
padding: 4,
}}
numberOfLines={2}
onChangeText={setMessage}
selectionColor={"white"}
placeholderTextColor={"#9FA0A7"}
/>
</View>
<View
style={{
justifyContent: "center",
alignItems: "center",
backgroundColor: message.length > 0 ? "#5568FE" : "transparent",
margin: 4,
padding: 4,
borderRadius: 8,
}}
>
{/* Button to send the message */}
<TouchableOpacity
onPress={sendMessage}
style={{
height: 40,
aspectRatio: 1,
justifyContent: "center",
alignItems: "center",
paddingVertical: 8,
paddingVertical: 4,
}}
>
<Text>Send</Text>
</TouchableOpacity>
</View>
</View>
);
};
return <>{textInput()}</>;
}
Private Chat
In the following example, to convert the chat into a private conversation between two participants, you can set the sendOnly
property. This property ensures that messages are only sent to the intended recipient, making the chat interaction exclusive and private between the two users involved.
import { SafeAreaView, TouchableOpacity, TextInput, Text } from "react-native";
function ChatView() {
// destructure publish method from usePubSub hook
const { publish, messages } = usePubSub("CHAT");
// State to store the user typed message
const [message, setMessage] = useState("");
const handleSendMessage = () => {
// Sending the Message using the publish method
// Pass the participantId of the participant to whom you want to send the message.
// hightlight-next-line
publish(message, { persist: true, sendOnly: ['XYZ'] });
// Clearing the message input
setMessage("");
};
//...
}
Downloading Chat Messages
All the messages from PubSub published persist : true
can be downloaded as an .csv
file. This file will be accessible in the VideoSDK dashboard and through the Sessions API.
✨ Want to Add More Features to React Native Video Calling App?
If you found this guide helpful and want to explore more features for your React Native video-calling app,
Check out these additional resources:
- Active Speaker Indication: Link
- RTMP Live Stream: Link
- Image Capture Feature: Link
- Screen Share Feature in Android: Link
- Screen Share Feature in iOS: Link
- Picture-in-Picture (PiP) Mode: Link
Conclusion
Congratulations!, you have integrated Screen Sharing successfully and unlocked the full potential of real-time communication, enabling users to engage in seamless conversations during video calls. Integrating a chat feature into a React Native video call app using VideoSDK can significantly enhance the user experience.
If you are new here and want to build an interactive React Native app with free resources, you can Sign up with VideoSDK and get ? 10000 free minutes every month. This will help your new video-calling app go to the next level without any costs associated with initial usage, allowing you to focus on building and scaling your application effectively.