Coder Social home page Coder Social logo

shankulkarni / react-native-hms Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 100mslive/100ms-react-native

0.0 0.0 0.0 11.67 MB

React Native Video Conferencing SDK & Sample App

License: MIT License

Java 3.11% JavaScript 0.88% Swift 17.36% C 0.05% Objective-C 2.06% Ruby 0.83% TypeScript 57.34% Starlark 0.28% Kotlin 18.08%

react-native-hms's Introduction

react-native-hms

npm license quality vulnerabilities collaborators Documentation Discord Firebase TestFlight Register

React native wrapper for 100ms SDK

Installation

npm install @100mslive/react-native-hms --save

cd ios/ && pod install

๐Ÿ“ฒ Download the Sample iOS App here: https://testflight.apple.com/join/v4bSIPad

๐Ÿค– Download the Sample Android App here: https://appdistribution.firebase.dev/i/7b7ab3b30e627c35

Permissions

For iOS Permissions

Add following lines in Info.plist file

<key>NSCameraUsageDescription</key>
<string>Please allow access to Camera to enable video conferencing</string>
<key>NSLocalNetworkUsageDescription</key>
<string>Please allow access to network usage to enable video conferencing</string>
<key>NSMicrophoneUsageDescription</key>
<string>Please allow access to Microphone to enable video conferencing</string>

For Android Permissions

Add following permissions in AndroidManifest.xml

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

You will also need to request Camera and Record Audio permissions at runtime before you join a call or display a preview. Please follow Android Documentation for runtime permissions.

We suggest using react-native-permission to acquire permissions from both platforms.

QuickStart

The package exports four Classes and an HMSManager class that manages everything.

Setting up the HMS Instance:

first we'll have to call build method, that method returns an instance of HMSManager class and the same is used to perform all the operations

import HmsManager from 'react-native-hms';
...

const hmsInstance = await HmsManager.build();
//save this instance, will be used for all the operations that we'll perform

...

Add event listeners

add event listeners for all the events such as onPreview, onJoin, onPeerUpdate etc. the actions can be found in HMSUpdateListenerActions class

import HmsManager, {
  HMSUpdateListenerActions,
} from 'react-native-hms';
...

// instance acquired from build() method
hmsInstance.addEventListener(
  HMSUpdateListenerActions.ON_PREVIEW,
  previewSuccess, // function that will be called on Preview success
);

...

The event handlers are the way of handling any update happening in hms all events can be found in HMSUpdateListenerActions class

Join the room

Joining the room connects you to the remote peer and broadcasts your stream to other peers, we need instance of HMSConfig in order to pass the details of room and user to join function

import HmsManager, {
  HMSUpdateListenerActions,
  HMSConfig,
} from 'react-native-hms';
...

// instance acquired from build() method
const HmsConfig = new HMSConfig({authToken, userID, roomID});
instance.preview(HmsConfig); // to start preview
// or 
instance.join(HmsConfig); // to join a room

...

don't forget to add ON_JOIN listener before calling join to receive an event callback

Viewing the video of a peer

To display a video on screen the package provide a UI component named HmsView that takes the video track ID and displays the video in that component, this component requires on width and height in style prop to set bounds of the tile that will show the video stream

...

//getting local track ID
const localTrackId = instance?.localPeer?.videoTrack?.trackId;

// getting remote track IDs
const remotePeers = instance?.remotePeers
const remoteVideoIds: string[] = [];

remotePeers.map((remotePeer: any) => {
  const remoteTrackId = remotePeer?.videoTrack?.trackId;

  if (remoteTrackId) {
    remoteVideoIds.push(remoteTrackId);
  }
});

...

Display a video in HmsView

import { HmsView } from 'react-native-hms';

...
const styles = StyleSheet.create({
  hmsView: {
    height: '100%',
    width: '100%',
  },
});

// trackId can be acquired from the method explained above
<HmsView style={styles.hmsView} trackId={trackId} />

...

Calling various functions of HMS

// Mute Audio
instance.localPeer.localAudioTrack().setMute(isMute);

// Stop Video
instance.localPeer.localVideoTrack().setMute(muteVideo);

// Switch Camera
instance.localPeer.localVideoTrack().switchCamera();

// Leave the call
instance.leave()

Sending messages

import { HMSMessage } from '@100mslive/react-native-hms';
  
// message object
const message = new HMSMessage({
  type: 'chat',
  time: new Date().toISOString(),
  message: value,
});
  
// send a message
instance.send(message);
  

Error handling

// import actions
import { HMSUpdateListenerActions } from '@100mslive/react-native-hms';
  
// add a event listener
instance.addEventListener(HMSUpdateListenerActions.ON_ERROR, onError);
  

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.