Integrating Agora SDK in React Native CLI: A Step-by-Step Guide

Real-time communication is at the core of many modern applications, enabling features like video conferencing, live streaming, and interactive broadcasts. Agora is a powerful platform that provides high-quality, real-time audio and video services, making it an ideal choice for developers who want to add these capabilities to their applications.

In this guide, I’ll walk you through the step-by-step process of integrating the Agora SDK in React Native CLI. Whether you’re building a new app or enhancing an existing one, this tutorial will help you leverage Agora’s features effectively.

Prerequisites

Before diving in, make sure you have:

  • Node.js installed (preferably the latest LTS version).
  • React Native CLI is already set up.
  • Android Studio and Xcode for Android and iOS development, respectively.
  • An Agora account with a project was created to obtain your App ID.

Step 1: Create a React Native Project

npx react-native init AgoraIntegrationDemo

Step 2: Install Agora Dependencies

You will need to install the react-native-agora library and the necessary peer dependencies:

npm install react-native-agora

Step 3: Configure Permissions

Agora requires certain permissions to function, especially for audio and video. Add the following permissions to your Android and iOS projects:

For Android:

Open android/app/src/main/AndroidManifest.xml and add these permissions inside the <manifest> tag:

Permissions inside the <manifest> tag:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.BLUETOOTH" />

For iOS:

Open ios/AgoraIntegrationDemo/Info.plist and add the following keys:

<key>NSCameraUsageDescription</key>
<string>Camera access is required for video calls.</string>
<key>NSMicrophoneUsageDescription</key>
<string>Microphone access is required for audio calls.</string>

Step 4: Generate Agora Credentials

Log in to the Agora Console, create a new project, and get your App ID. You will need this App ID to initialize the Agora SDK in React Native CLI.

Explore Our Mobile App Development Services for Live Interaction

Step 5: Implement Basic Video Call

Let’s implement a basic video call to test the integration:

➡️ Import Agora SDK

In the file where you want to add Agora video calling (e.g., App.js), start by importing Agora:

import RtcEngine, { RtcLocalView, RtcRemoteView, VideoRenderMode } from 'react-native-agora';
import React, { useEffect, useState } from 'react';
import { View, Button, PermissionsAndroid, Platform } from 'react-native';

➡️ Set Up State and RTC Engine

Initialize the state and the Agora RTC engine in the component:

const App = () => {
 const [engine, setEngine] = useState(null);
 const [joined, setJoined] = useState(false);
 const [remoteUid, setRemoteUid] = useState(null);
 const APP_ID = 'YOUR_AGORA_APP_ID';
 const CHANNEL_NAME = 'testChannel';

 useEffect(() => {
  const init = async () => {
    if (Platform.OS === 'android') {
       await PermissionsAndroid.requestMultiple([
        PermissionsAndroid.PERMISSIONS.CAMERA,
        PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
       ]);
    }

    const rtcEngine = await RtcEngine.create(APP_ID);
    setEngine(rtcEngine);

    rtcEngine.addListener('UserJoined', (uid) => {
     setRemoteUid(uid);
    });

    rtcEngine.addListener('JoinChannelSuccess', () => {
     setJoined(true);
    });

    await rtcEngine.enableVideo();
    await rtcEngine.joinChannel(null, CHANNEL_NAME, null, 0);
   };
   init();

   return () => {
     engine?.destroy();
   };
  }, []);

  return (
   <View style={{ flex: 1 }}>
    {joined ? (
      <>
       <RtcLocalView.SurfaceView
        style={{ flex: 1 }}
        channelId={CHANNEL_NAME}
        renderMode={VideoRenderMode.Hidden}
       />
       {remoteUid && (
        <RtcRemoteView.SurfaceView
        style={{ flex: 1 }}
        uid={remoteUid}
        channelId={CHANNEL_NAME}
        renderMode={VideoRenderMode.Hidden}
        />
       )}
      </>
     ) : (
       <Button title="Join Channel" onPress={() => engine?.joinChannel(null, CHANNEL_NAME, null, 0)} />
     )}
   </View>
 );
};

export default App;

Step 6: Run the Application

After the code is implemented, it’s time to run the application on Android or iOS:

For Android:

npx react-native run-android

For iOS:

npx react-native run-ios
coma

Conclusion

Integrating the Agora SDK in React Native CLI enables real-time communication with minimal effort. Agora’s video and audio capabilities are highly scalable and suitable for projects of all sizes, making it a great choice for your app.

I hope this guide helps you get started with adding live communication to your app! Feel free to experiment further with Agora’s API to build more interactive features.

Keep Reading

Keep Reading

  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?