<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Enrico Portolan</title>
    <description>The latest articles on Forem by Enrico Portolan (@enricop89).</description>
    <link>https://forem.com/enricop89</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F494929%2F09be0fd4-d9b0-4475-893b-e90c10c8b151.jpeg</url>
      <title>Forem: Enrico Portolan</title>
      <link>https://forem.com/enricop89</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/enricop89"/>
    <language>en</language>
    <item>
      <title>Build a Video Conference App With React-Native and Vonage Video API</title>
      <dc:creator>Enrico Portolan</dc:creator>
      <pubDate>Mon, 12 Oct 2020 13:21:50 +0000</pubDate>
      <link>https://forem.com/vonagedev/build-a-video-conference-app-with-react-native-and-vonage-video-api-4c3d</link>
      <guid>https://forem.com/vonagedev/build-a-video-conference-app-with-react-native-and-vonage-video-api-4c3d</guid>
      <description>&lt;p&gt;Having video call functionality in your application seems to be a must-have these days. However, building a native application from scratch and handling Android and iOS different capabilities can be a very long and expensive process.&lt;/p&gt;

&lt;p&gt;This article will explain how to build a React Native application with video capabilities by leveraging the &lt;a href="https://github.com/opentok/opentok-react-native" rel="noopener noreferrer"&gt;Opentok React Native&lt;/a&gt; library. For a demonstration of how the application will function, check out the video below:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Want to jump ahead? You can find the code for this tutorial on &lt;a href="https://github.com/enricop89/multiparty-video-react-native.git" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A Vonage Video API account. If you don’t have one already, you can create an account in the &lt;a href="https://tokbox.com/account/#/" rel="noopener noreferrer"&gt;Video Dashboard&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Beginner-level React-Native experience&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Vonage Video API
&lt;/h3&gt;

&lt;p&gt;The Vonage Video API is a WebRTC based solution. It supports many platforms, among those are: browsers, Android, iOS, Windows and macOS. We will use the API to create live video sessions between our different roles. You can read more about the API on the &lt;a href="https://www.vonage.com/developer-center/?icmp=mainnav_developercenter_novalue" rel="noopener noreferrer"&gt;Vonage Video Developer Center&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once you have an account, the next step is to create a &lt;code&gt;New Project&lt;/code&gt; -&amp;gt; &lt;code&gt;OpenTokAPI&lt;/code&gt; with default values (VP8 as default codec).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.nexmo.com/wp-content/uploads/2020/10/opentok-create-account.png" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.nexmo.com%2Fwp-content%2Fuploads%2F2020%2F10%2Fopentok-create-account.png" alt="opentok create account" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  React Native
&lt;/h3&gt;

&lt;p&gt;React Native is an open-source mobile application framework, developed by Facebook. React Native concepts are the same with React, but they’re targeting mobile apps instead of targeting the browser.&lt;/p&gt;

&lt;p&gt;You can find an introduction and getting started tutorials on the official &lt;a href="https://reactnative.dev/docs/getting-started" rel="noopener noreferrer"&gt;React Native&lt;/a&gt; page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explore the Project
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Setup
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Clone the following repo: &lt;a href="https://github.com/enricop89/multiparty-video-react-native.git" rel="noopener noreferrer"&gt;https://github.com/enricop89/multiparty-video-react-native.git&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Install the required node modules: &lt;code&gt;npm install&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  For iOS
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Install the Podfile’s dependencies: &lt;code&gt;cd ios/ &amp;amp;&amp;amp; pod install&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Project Structure
&lt;/h3&gt;

&lt;p&gt;The project structure is composed of the following main files:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;Android&lt;/code&gt; and &lt;code&gt;iOS&lt;/code&gt; folders: is where the native code is compiled and executed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;App.js&lt;/code&gt;: is the main file, where the React state, props, event handlers and methods are defined.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;config.js&lt;/code&gt;: is where the credentials are defined.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The App component will be our landing view when our App is launched. It will have a primary conditional render function to start the video call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class App extends Component {
  constructor(props) {
    super(props);
    this.apiKey = credentials.API_KEY;
    this.sessionId = credentials.SESSION_ID;
    this.token = credentials.TOKEN;
    this.state = {
      subscriberIds: [], // Array for storing subscribers
      localPublishAudio: true, // Local Audio state
      localPublishVideo: true, // Local Video state
      joinCall: false, // State variable used to start the call
      streamProperties: {}, // Handle individual stream properties,
      mainSubscriberStreamId: null
    }; 
  }

  joinCall = () =&amp;gt; {
    const { joinCall } = this.state;
    if (!joinCall) {
      this.setState({ joinCall: true });
    }
  };

  endCall = () =&amp;gt; {
    const { joinCall } = this.state;
    if (joinCall) {
      this.setState({ joinCall: !joinCall });
    }
  };

  joinVideoCall = () =&amp;gt; {
    return (
      &amp;lt;View style={styles.fullView}&amp;gt;
        &amp;lt;Button
          onPress={this.joinCall}
          title="JoinCall"
          color="#841584"
          accessibilityLabel="Join call"&amp;gt;
          Join Call
        &amp;lt;/Button&amp;gt;
      &amp;lt;/View&amp;gt;
    );
  };

  render() {
    return this.state.joinCall ? this.videoPartyView() : this.joinVideoCall();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;joinCall&lt;/code&gt; property on the React state will trigger different views based on the value. When the App is launched, a simple View with a “Join the Call” button will be displayed to the user. The button will trigger a state change and toggle the &lt;code&gt;joinCall&lt;/code&gt; value to show the more complex videocall view.&lt;/p&gt;

&lt;h4&gt;
  
  
  Videocall View
&lt;/h4&gt;

&lt;p&gt;Before digging into the videocall View, it’s worth spending some time exploring the &lt;code&gt;opentok-react-native&lt;/code&gt; library.&lt;/p&gt;

&lt;p&gt;The library is composed of three main components: &lt;code&gt;OTSession&lt;/code&gt;, &lt;code&gt;OTPublisher&lt;/code&gt; and &lt;code&gt;OTSubscriber&lt;/code&gt;. Each of them will interact with the native layer (iOS and Android), calling the native methods to connect, publish and subscribe. We will also need to listen to the events fired by those components, especially the session events, such as &lt;code&gt;sessionConnected&lt;/code&gt;, &lt;code&gt;sessionDisconnected&lt;/code&gt;, &lt;code&gt;streamCreated&lt;/code&gt; and &lt;code&gt;streamDestroyed&lt;/code&gt; (Documentation: &lt;a href="https://github.com/opentok/opentok-react-native/blob/master/docs/OTSession.md#events" rel="noopener noreferrer"&gt;Session Events&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;The videocall View is composed of the following components: &lt;strong&gt;Publisher&lt;/strong&gt; , &lt;strong&gt;Subscribers&lt;/strong&gt; and a &lt;strong&gt;Toolbar&lt;/strong&gt; on the bottom side to control the microphone and camera and to end the call.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.nexmo.com/wp-content/uploads/2020/10/multiparty-mockup.png" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.nexmo.com%2Fwp-content%2Fuploads%2F2020%2F10%2Fmultiparty-mockup.png" alt="multiparty-mockup" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To build the above View, we need to keep track of the subscribers’ streams, the primary subscriber, and the local microphone and camera publishing state. The perfect place to store this information is the React State.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class App extends Component {
  constructor(props) {
    super(props);
    this.apiKey = credentials.API_KEY;
    this.sessionId = credentials.SESSION_ID;
    this.token = credentials.TOKEN;
    this.state = {
      subscriberIds: [], // Array for storing subscribers
      localPublishAudio: true, // Local Audio state
      localPublishVideo: true, // Local Video state
      joinCall: false, // State variable used to start the call
      streamProperties: {}, // Handle individual stream properties,
      mainSubscriberStreamId: null
    }; 
  }

  joinCall = () =&amp;gt; {
    const { joinCall } = this.state;
    if (!joinCall) {
      this.setState({ joinCall: true });
    }
  };

  endCall = () =&amp;gt; {
    const { joinCall } = this.state;
    if (joinCall) {
      this.setState({ joinCall: !joinCall });
    }
  };

  joinVideoCall = () =&amp;gt; {
    return (
      &amp;lt;View style={styles.fullView}&amp;gt;
        &amp;lt;Button
          onPress={this.joinCall}
          title="JoinCall"
          color="#841584"
          accessibilityLabel="Join call"&amp;gt;
          Join Call
        &amp;lt;/Button&amp;gt;
      &amp;lt;/View&amp;gt;
    );
  };

  render() {
    return this.state.joinCall ? this.videoPartyView() : this.joinVideoCall();
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;SubscriberIds&lt;/code&gt; array stores the subscribers within a session. Each time we receive a &lt;strong&gt;streamCreated&lt;/strong&gt; event, it means that someone has joined the session and published a stream, so we need to add their streamId on the &lt;code&gt;subscriberIds&lt;/code&gt; array.&lt;br&gt;&lt;br&gt;
On the other hand, when we received the &lt;code&gt;streamDestroyed&lt;/code&gt; event, we need to remove the streamId from the subscribers’ array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;View style={styles.fullView}&amp;gt;
      &amp;lt;OTSession
        apiKey={this.apiKey}
        sessionId={this.sessionId}
        token={this.token}
        eventHandlers={this.sessionEventHandlers}&amp;gt;
        &amp;lt;OTPublisher
          properties={this.publisherProperties}
          eventHandlers={this.publisherEventHandlers}
          style={styles.publisherStyle}
        /&amp;gt;
        &amp;lt;OTSubscriber style={{ height: dimensions.height, width: dimensions.width }}
          eventHandlers={this.subscriberEventHandlers}
          streamProperties={this.state.streamProperties}
        &amp;gt;
          {this.renderSubscribers}
        &amp;lt;/OTSubscriber&amp;gt;
      &amp;lt;/OTSession&amp;gt;
    &amp;lt;/View&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the video view render function, we need to add &lt;code&gt;OTSession&lt;/code&gt;, &lt;code&gt;OTPublisher&lt;/code&gt; and &lt;code&gt;OTSubscriber&lt;/code&gt; from the &lt;code&gt;opentok-react-native&lt;/code&gt; library. On the &lt;code&gt;OTSession&lt;/code&gt; we set the credentials and the eventHandler function as props of the component.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;OTPublisher&lt;/code&gt; component will initialize a publisher and publish to the specified session upon mounting. It’s possible to specify different properties, such as camera position, resolution, and others (Publisher options list: &lt;a href="https://github.com/opentok/opentok-react-native/blob/master/docs/OTPublisher.md#properties" rel="noopener noreferrer"&gt;Publisher Options&lt;/a&gt;). In this example App, we will only set &lt;code&gt;this.publisherProperties = { cameraPosition: 'front'};&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Ensure you have enabled both camera and microphone usage by adding the following entries to your &lt;code&gt;Info.plist&lt;/code&gt; file (iOS Project):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;key&amp;gt;NSCameraUsageDescription&amp;lt;/key&amp;gt;
&amp;lt;string&amp;gt;Your message to user when the camera is accessed for the first time&amp;lt;/string&amp;gt;
&amp;lt;key&amp;gt;NSMicrophoneUsageDescription&amp;lt;/key&amp;gt;
&amp;lt;string&amp;gt;Your message to user when the microphone is accessed for the first time&amp;lt;/string&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, for Android, add the following (newer versions of Android–&lt;code&gt;API Level 23&lt;/code&gt; (Android 6.0)–have a different permissions model that is already handled by this library):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;uses-permission android:name="android.permission.CAMERA" /&amp;gt;
&amp;lt;uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /&amp;gt;
&amp;lt;uses-permission android:name="android.permission.RECORD_AUDIO" /&amp;gt;
&amp;lt;uses-feature android:name="android.hardware.camera" android:required="true" /&amp;gt;
&amp;lt;uses-feature android:name="android.hardware.camera.autofocus" android:required="false" /&amp;gt;
&amp;lt;uses-feature android:name="android.hardware.microphone" android:required="true" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;OTPublisher&lt;/code&gt; component also has a &lt;code&gt;streamProperty&lt;/code&gt; property which handles the publisher properties passed into the native instance. Using the React State, we can trigger changes to the Publisher instance by updating the &lt;code&gt;this.publisherProperties&lt;/code&gt; variable. We use this approach to implement the Toolbar with the mute/unmute functions for the Microphone and Camera. The function implementation is straightforward; it toggles the &lt;strong&gt;publishAudio&lt;/strong&gt; or &lt;strong&gt;publishVideo&lt;/strong&gt; value on the &lt;code&gt;this.publisherProperties&lt;/code&gt; and the &lt;code&gt;localPublishAudio&lt;/code&gt; and &lt;code&gt;localPublishVideo&lt;/code&gt; to adjust the button icon based on the value.&lt;/p&gt;

&lt;p&gt;The End Call button has a very similar approach. The &lt;code&gt;endCall&lt;/code&gt; function toggles the &lt;code&gt;joinCall&lt;/code&gt; value on the State and resets the View to the initial one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;toggleAudio = () =&amp;gt; {
    let publishAudio = this.state.localPublishAudio;
    this.publisherProperties = { ...this.publisherProperties, publishAudio: !publishAudio };
    this.setState({
      localPublishAudio: !publishAudio,
    });
  };

  toggleVideo = () =&amp;gt; {
    let publishVideo = this.state.localPublishVideo;
    this.publisherProperties = { ...this.publisherProperties, publishVideo: !publishVideo };
    this.setState({
      localPublishVideo: !publishVideo,
    });
  };

 endCall = () =&amp;gt; {
    const { joinCall } = this.state;
    if (joinCall) {
      this.setState({ joinCall: !joinCall });
    }
  };

&amp;lt;View style={styles.buttonView}&amp;gt;
  &amp;lt;Icon.Button
    style={styles.iconStyle}
    backgroundColor="#131415"
    name={this.state.localPublishAudio ? 'mic' : 'mic-off'}
    onPress={this.toggleAudio}
  /&amp;gt;
  &amp;lt;Icon.Button
    style={styles.iconStyle}
    backgroundColor="#131415"
    name="call-end"
    onPress={this.endCall}
  /&amp;gt;
  &amp;lt;Icon.Button
    style={styles.iconStyle}
    backgroundColor="#131415"
    name={this.state.localPublishVideo ? 'videocam' : 'videocam-off'}
    onPress={this.toggleVideo}
  /&amp;gt;
&amp;lt;/View&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have reached this point, we have implemented the Join Call View, the Session and Publisher component, and the Toolbar. Next, we will define the View for the different possible number of subscribers. If we have &lt;strong&gt;no users&lt;/strong&gt; , we are going to display a simple informative text.&lt;/p&gt;

&lt;p&gt;If we have only &lt;strong&gt;one subscriber&lt;/strong&gt; , we will display their stream in full-screen mode. Finally, if we have &lt;strong&gt;more than one user&lt;/strong&gt; , we will show the primary subscriber in the big View (as shown in the mock-up), and the other in a Scroll View component to handle a different number of subscribers. As the number could grow and challenge our device CPU and network bandwidth, we will implement optimizations on each of the subscribers, such as lowering the resolution and disabling the video for the subscribers that are not visible.&lt;/p&gt;

&lt;p&gt;Let’s explore the &lt;code&gt;OTSubscriber&lt;/code&gt; component to handle the cases described above. First of all, as we want to have control over each subscriber, we would need to implement a render function for the subscribers (&lt;a href="https://github.com/opentok/opentok-react-native/blob/master/docs/OTSubscriber.md#custom-rendering-of-streams" rel="noopener noreferrer"&gt;custom-rendering-of-streams&lt;/a&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;renderSubscribers = (subscribers) =&amp;gt; {
    if (this.state.mainSubscriberStreamId) {
      subscribers = subscribers.filter(sub =&amp;gt; sub !== this.state.mainSubscriberStreamId);
      subscribers.unshift(this.state.mainSubscriberStreamId);
    }
    return subscribers.length &amp;gt; 1 ? (
      &amp;lt;&amp;gt;
        &amp;lt;View style={styles.mainSubscriberStyle}&amp;gt;
          &amp;lt;TouchableOpacity
            onPress={() =&amp;gt; this.handleSubscriberSelection(subscribers, subscribers[0])}
            key={subscribers[0]}&amp;gt;
            &amp;lt;OTSubscriberView
              streamId={subscribers[0]}
              style={{
                width: '100%', height: '100%'
              }}
            /&amp;gt;
          &amp;lt;/TouchableOpacity&amp;gt;
        &amp;lt;/View&amp;gt;

        &amp;lt;View style={styles.secondarySubscribers}&amp;gt;
          &amp;lt;ScrollView
            horizontal={true}
            decelerationRate={0}
            snapToInterval={dimensions.width / 2}
            snapToAlignment={'center'}
            onScrollEndDrag={(e) =&amp;gt; this.handleScrollEnd(e, subscribers.slice(1))}
            style={{
              width: dimensions.width,
              height: dimensions.height / 4,
            }}&amp;gt;
            {subscribers.slice(1).map((streamId) =&amp;gt; (
              &amp;lt;TouchableOpacity
                onPress={() =&amp;gt; this.handleSubscriberSelection(subscribers, streamId)}
                style={{
                  width: dimensions.width / 2,
                  height: dimensions.height / 4,
                }}
                key={streamId}
              &amp;gt;
                &amp;lt;OTSubscriberView
                  style={{
                    width: '100%', height: '100%'
                  }}
                  key={streamId}
                  streamId={streamId}
                /&amp;gt;
              &amp;lt;/TouchableOpacity&amp;gt;
            ))}
          &amp;lt;/ScrollView&amp;gt;
        &amp;lt;/View&amp;gt;
      &amp;lt;/&amp;gt;
    ) : subscribers.length &amp;gt; 0 ? (
      &amp;lt;TouchableOpacity style={styles.fullView}&amp;gt;
        &amp;lt;OTSubscriberView streamId={subscribers[0]}
          key={subscribers[0]}
          style={{ width: '100%', height: '100%' }}
        /&amp;gt;
      &amp;lt;/TouchableOpacity&amp;gt;
    ) : (&amp;lt;Text&amp;gt;No one connected&amp;lt;/Text&amp;gt;)
  }; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use the conditional rendering in React (&lt;a href="https://reactjs.org/docs/conditional-rendering.html" rel="noopener noreferrer"&gt;https://reactjs.org/docs/conditional-rendering.html&lt;/a&gt;) to handle the different cases with zero, one or N subscribers.&lt;/p&gt;

&lt;p&gt;Firstly, if there are not subscribers, we fall into the last case, and we display a &lt;code&gt;Text&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;Secondly, if there is one subscriber, we display the subscriber in a full view mode.&lt;/p&gt;

&lt;p&gt;Lastly, the most interesting case, when the subscribers are more than one:&lt;br&gt;&lt;br&gt;
We have a main subscriber view and a &lt;code&gt;ScrollView&lt;/code&gt; component in which we will feed the other subscribers. The first step is to check if we have a &lt;code&gt;mainSubscriberStreamId&lt;/code&gt;. If so, we will sort the array to have the primary subscriber as the first element. The remaining subscribers will be displayed in the &lt;code&gt;ScrollView&lt;/code&gt; horizontal. The &lt;code&gt;ScrollView&lt;/code&gt; component is ideal for our use case, as we can show a relatively high number of subscribers without the need to change the layout, and we can detect how many subscribers are in the scroll view and how many of them are &lt;strong&gt;visible&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Group calls on mobile devices could be very challenging, both from the hardware and network point of view. To deliver a good result to the end-user, an App should implement a list of best practices to handle different use cases and layout. In our case, we have a main subscriber view which needs to have the best resolution possible, and the Scroll View component with the remaining subscribers in smaller thumbnails that could be optimized by lowering the received resolution. Opentok SDKs give the developer the opportunity to set the preferred resolution and frame rate for each of the subscriber (&lt;a href="https://tokbox.com/developer/sdks/js/reference/Subscriber.html#setPreferredFrameRate" rel="noopener noreferrer"&gt;setPreferredFrameRate&lt;/a&gt; and &lt;a href="https://tokbox.com/developer/sdks/js/reference/Subscriber.html#setPreferredResolution" rel="noopener noreferrer"&gt;setPreferredResolution&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;We implement the &lt;code&gt;handleSubscriberSelection&lt;/code&gt; method to handle the mainSubscriber View and the preferred resolution. The function is on the &lt;code&gt;TouchableOpacity&lt;/code&gt; component parent of each of the subscribers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mainSubscribersResolution = { width: 1280, height: 720 };
const secondarySubscribersResolution = { width: 352, height: 288 };


  handleSubscriberSelection = (subscribers, streamId) =&amp;gt; {
    let subscriberToSwap = subscribers.indexOf(streamId);
    let currentSubscribers = subscribers;
    let temp = currentSubscribers[subscriberToSwap];
    currentSubscribers[subscriberToSwap] = currentSubscribers[0];
    currentSubscribers[0] = temp;
    this.setState(prevState =&amp;gt; {
      const newStreamProps = { ...prevState.streamProperties };
      for (let i = 0; i &amp;lt; currentSubscribers.length; i += 1) {
        if (i === 0) {
          newStreamProps[currentSubscribers[i]] = { ...prevState.streamProperties[currentSubscribers[i]] }
          newStreamProps[currentSubscribers[i]].preferredResolution = mainSubscribersResolution;
        } else {
          newStreamProps[currentSubscribers[i]] = { ...prevState.streamProperties[currentSubscribers[i]] }
          newStreamProps[currentSubscribers[i]].preferredResolution = secondarySubscribersResolution;
        }
      }
      return { mainSubscriberStreamId: streamId, streamProperties: newStreamProps };
    })
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Based on the subscriber selected, the function moves the selected subscriber to the head of the subscribers’ array. As mentioned before, the first element on the subscriber array will be displayed in the main View. After that, we need to update the &lt;code&gt;streamProperties&lt;/code&gt; of the &lt;code&gt;OTSubscriber&lt;/code&gt; component to set the different preferred resolution. We set the maximum resolution (&lt;code&gt;width: 1280, height: 720&lt;/code&gt;) for the primary subscriber and a lower resolution for the others (&lt;code&gt;{ width: 352, height: 288 }&lt;/code&gt;). If we also want to change the preferred frame rate, based on the layout or use case, we would only need to add the &lt;code&gt;preferredFrameRate&lt;/code&gt; property on the &lt;code&gt;streamProperties&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;Finally, we want to optimize the &lt;code&gt;ScrollView&lt;/code&gt; component. The ScrollView component could have a high number of subscribers, but can only show two simultaneously. For example, if we have five subscribers, one will be on the main subscriber view; the remaining four will be on the ScrollView. Only two of them are visible in the View, and the remaining ones will be visible only if we scroll horizontally.&lt;/p&gt;

&lt;p&gt;The ScrollView component has an event listener called &lt;a href="https://reactnative.dev/docs/scrollview#onscrollenddrag" rel="noopener noreferrer"&gt;onScrollEndDrag&lt;/a&gt;, which is called when the user stops dragging the scroll view and it either stops or begins to glide. We can use this event to understand which subscribers are visible and mute the video of the remaining ones. Muting the video of the non-visible stream will improve the performance of the App, and save CPU consumption and network bandwidth.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;handleScrollEnd = (event, subscribers) =&amp;gt; {
    let firstVisibleIndex;
    if (event &amp;amp;&amp;amp; event.nativeEvent &amp;amp;&amp;amp; !isNaN(event.nativeEvent.contentOffset.x)) {
      firstVisibleIndex = parseInt(event.nativeEvent.contentOffset.x / (dimensions.width / 2), 10);
    }
    this.setState(prevState =&amp;gt; {
      const newStreamProps = { ...prevState.streamProperties };
      if (firstVisibleIndex !== undefined &amp;amp;&amp;amp; !isNaN(firstVisibleIndex)) {
        for (let i = 0; i &amp;lt; subscribers.length; i += 1) {
          if (i === firstVisibleIndex || i === (firstVisibleIndex + 1)) {
            newStreamProps[subscribers[i]] = { ...prevState.streamProperties[subscribers[i]] }
            newStreamProps[subscribers[i]].subscribeToVideo = true;
          } else {
            newStreamProps[subscribers[i]] = { ...prevState.streamProperties[subscribers[i]] }
            newStreamProps[subscribers[i]].subscribeToVideo = false;
          }
        }
      }
      return { streamProperties: newStreamProps }
    })
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the &lt;code&gt;onScrollEndDrag&lt;/code&gt; event, we have the information about the &lt;strong&gt;contentOffset&lt;/strong&gt; coordinates, which is &lt;em&gt;the point at which the origin of the content view is offset from the origin of the scroll view&lt;/em&gt;. We will use this value to understand which streams are currently visible, dividing the content offset by half of the width of the screen (&lt;code&gt;event.nativeEvent.contentOffset.x / (dimensions.width / 2)&lt;/code&gt;).&lt;br&gt;&lt;br&gt;
The result will be the &lt;strong&gt;first visible subscriber&lt;/strong&gt;. At this point, we know that the visible streams are the stream in position &lt;code&gt;firstVisibleIndex&lt;/code&gt; and &lt;code&gt;firstVisibleIndex + 1&lt;/code&gt;. The last step is to loop the subscribers’ array and mute the video of the non-visible subscribers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Since the pandemic hit, video call apps have become a well-integrated habit in our lives. This blog post shows how incredibly fast and straightforward it can be to integrate video call functionality using React Native and Vonage Video API SDKs. If you have any questions or comments, please reach out to us on &lt;a href="https://twitter.com/VonageDev" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; or join our &lt;a href="https://developer.nexmo.com/community/slack" rel="noopener noreferrer"&gt;Community Slack Channel&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/enricop89/multiparty-video-react-native.git" rel="noopener noreferrer"&gt;https://github.com/enricop89/multiparty-video-react-native.git&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The post &lt;a href="https://www.nexmo.com/blog/2020/10/12/build-a-video-conference-app-with-react-native-and-vonage-video-api" rel="noopener noreferrer"&gt;Build a Video Conference App With React-Native and Vonage Video API&lt;/a&gt; appeared first on &lt;a href="https://www.nexmo.com" rel="noopener noreferrer"&gt;Vonage Developer Blog&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>developer</category>
      <category>tutorial</category>
      <category>video</category>
      <category>reactnative</category>
    </item>
  </channel>
</rss>
