
opentok-react-native
In this repo, you'll find the OpenTok React Native library:
Pre-Requisites:
-
Install node.js
-
Install and update Xcode (you will need a Mac)
- Install and update Android Studio
Installation:
-
In your terminal, change into your React Native project's directory
-
In your terminal, run npm install opentok-react-native
iOS Installation
Note Please make sure to have CocoaPods on your computer.
-
In you terminal, change into your ios
directory.
-
Create a pod file by running: pod init
.
-
Add the following to your pod file:
platform :ios, '9.0'
target '<YourProjectName>' do
use_frameworks!
# Pods for <YourProject>
pod 'OpenTok'
end
-
Now run, pod install
-
Open XCode
-
Open .xcworkspace file in XCode. This file can be found in the ios
folder.
-
Click File
and Add Files to
-
Add the following files to the project:
OTPublisher.h
OTPublisher.m
OTPublisherManager.swift
OTPublisherView.swift
OTRN.swift
OTSessionManager.m
OTSessionManager.swift
OTSubscriber.h
OTSubscriber.m
OTSubscriberManager.swift
OTSubscriberView.swift
<YourProjectName>-Bridging-Header.h
- Click
Create Bridging Header
when you're prompted with the following modal: Would you like to configure an Objective-C bridging header?
Android Installation
-
In you terminal, change into your project directory.
-
Run react-native link opentok-react-native
-
Open your Android project in Android Studio.
-
Add the following to your project build.gradle
file:
maven {
url "http://tokbox.bintray.com/maven"
}
- It should look something like this:

-
Sync Gradle
-
Make sure the following in your app's gradle compileSdkVersion
, buildToolsVersion
, minSdkVersion
, and targetSdkVersion
are the same in the OpenTok React Native library.
API Reference
The OpenTok React Native
library comprises of:
OTSession
ComponentOTPublisher
ComponentOTSubscriber
Component
OTSession Component
Prop | Type | Required | Description |
---|
apiKey | String | Yes | TokBox API Key |
sessionId | String | Yes | TokBox Session ID |
token | String | Yes | TokBox token |
signal | Object | No | Used to send a signal to the session |
eventHandlers | Object<Function> | No | Event handlers passed into the native session instance. |
The OTSession
component manages the connection to an OpenTok Session. It passes the sessionId to the sessionId
prop to its child components. To disconnect the session, unmount the OTSession
component. To publish and subscribe, you must nest OTPublisher
and OTSubscriber
inside OTSession
:
<OTSession apiKey="your-api-key" sessionId="your-session-id" token="your-session-token">
<OTPublisher style={{ width: 100, height: 100 }}/>
<OTSubscriber style={{ width: 100, height: 100 }} />
</OTSession>
OTPublisher Component
Prop | Type | Required | Description |
---|
sessionId | String | No | OpenTok sessionId. This is auto populated by wrapping OTPublisher with OTSession |
properties | Object | No | Properties passed into the native publisher instance |
eventHandlers | Object<Function> | No | Event handlers passed into native publsiher instance |
The OTPublisher
component will initialize a publisher and publish to a specified session upon mounting. To destroy the publisher, unmount the OTPublisher
component.
<OTSession apiKey="your-api-key" sessionId="your-session-id" token="your-session-token">
<OTPublisher style={{ width: 100, height: 100 }} />
</OTSession>
class App extends Component {
constructor(props) {
super(props);
this.publisherProperties = {
publishAudio: false,
cameraPosition: 'front'
};
this.publisherEventHandlers = {
streamCreated: event => {
console.log('Publisher stream created!', event);
},
streamDestroyed: event => {
console.log('Publisher stream destroyed!', event);
}
};
}
render() {
return (
<OTSession apiKey="your-api-key" sessionId="your-session-id" token="your-session-token">
<OTPublisher
properties={this.publisherProperties}
eventHandlers={this.publisherEventHandlers}
style={{ height: 100, width: 100 }}
/>
</OTSession>
);
}
}
The properties
prop is used for initial set up of the Publisher and making changes to it will update the Publisher. For convenience the OTPublisher
watches for changes on a few keys of the properties
object and makes the necessary changes. Currently these are:
Publisher Property | Action |
---|
publishAudio | Calls OT.publishAudio() to toggle audio on and off |
publishVideo | Calls OT.publishVideo() to toggle video on and off |
Please keep in mind that OT
is not the same as OT
in the JS SDK, the OT
in this library refers to the iOS and Android OTSessionManager
class.
OTSubscriber Component
Prop | Type | Required | Description |
---|
sessionId | String | No | OpenTok Session Id. This is auto populated by wrapping OTSubscriber with OTSession |
streamId | String | No | OpenTok Subscriber streamId. This is auto populated inside the OTSubscriber component when streamCreated event is fired from the native session delegate(iOS)/ interface(Android) |
properties | Object | No | Properties passed into the native subscriber instance |
eventHandlers | Object<Function> | No | Event handlers passed into the native subscriber instance |
The OTSubscriber
component will subscribe to a specified stream from a specified session upon mounting. The OTSubscriber
component will stop subscribing and unsubscribing when it's unmounting.
Contributing
If you make changes to the project that you would like to contribute back then please follow the contributing guidelines. All contributions are greatly appreciated!