
Security News
The Nightmare Before Deployment
Season’s greetings from Socket, and here’s to a calm end of year: clean dependencies, boring pipelines, no surprises.
react-use-opentok
Advanced tools
React Hook for conveniently use @opentok/client SDK.
See our demo site: react-use-opentok

Install it with npm:
npm i @opentok/client@2.x react-use-opentok
Or with yarn:
yarn add @opentok/client@2.x react-use-opentok
NOTE: remember to install the peer dependency of @opentok/client
useOpenTok hookapiKey, sessionId, and token from servertokenimport React, { useEffect } from 'react';
import useOpenTok from 'react-use-opentok';
const Component = () => {
// STEP 1: get utilities from useOpenTok;
const [opentokProps, opentokMethods] = useOpenTok();
const {
// connection info
isSessionInitialized,
connectionId,
isSessionConnected,
// connected data
session,
connections,
streams,
subscribers,
publisher,
} = opentokProps;
const {
initSessionAndConnect,
disconnectSession,
publish,
unpublish,
subscribe,
unsubscribe,
sendSignal,
} = opentokMethods;
// STEP 2: Mockup fetching apiKey, sessionId, and token from server
useEffect(() => {
fetch('<get credentials from server>').then(
({ apiKey, sessionId, token }) => {
initSessionAndConnect({
apiKey,
sessionId,
token,
});
}
);
}, [initSessionAndConnect]);
return <div>...</div>;
};
export default Component;
You can get all utilities from useOpenTok hook.
const [opentokProps, opentokMethods] = useOpenTok();
const {
// connection info
isSessionInitialized,
connectionId,
isSessionConnected,
// connected data
session,
connections,
streams,
subscribers,
publisher,
} = opentokProps;
const {
initSessionAndConnect,
disconnectSession,
publish,
unpublish,
subscribe,
unsubscribe,
sendSignal,
} = opentokMethods;
Before starting use openTok session object, remember to initialize session with apiKey and sessionId by initSessionAndConnect method:
const [opentokProps, opentokMethods] = useOpenTok();
const { initSessionAndConnect } = opentokMethods;
// apiKey, sessionId, and token could get from your server or tokbox dashboard
initSessionAndConnect({
apiKey,
sessionId,
token,
});
This methods will first initialize the session object, and continue connected to the session.
After session initialized, the value of isSessionInitialized will be `true.
After connect to session, you can get the session, connectionId , isSessionConnected, and connections properties from opentokProps:
session: a session object from OT.initSession()connectionId: your own connectionIdisSessionConnected: whether you are connected to the sessionconnections: all connections in the sessionconst [opentokProps, opentokMethods] = useOpenTok();
const { session, connectionId, isSessionConnected, connections } = opentokProps;
By disconnectSession, you can disconnect from the session:
const [opentokProps, opentokMethods] = useOpenTok();
const { disconnectSession } = opentokMethods;
disconnectSession();
If you want to control the process of session initialization and connect to session on your own, check the method
initSession({ apiKey, sessionId, sessionOptions })andconnectSession({token, sessionToConnect }).
You can publish stream from camera or screen to session through the publish method.
name: should be unique every time you invoke publish method which is for unpublish stream later.element: should be a DOM element or the id attribute of the existing DOM element.options: (optional) other optional properties which will pass into OT.initPublisher.const [opentokProps, opentokMethods] = useOpenTok();
const { publish } = opentokMethods;
// publish stream from the camera
publish({
name: 'camera',
element: 'camera',
});
// publish stream from screen sharing
publish({
name: 'screen',
element: 'screen',
options: {
insertMode: 'append',
width: '100%',
height: '100%',
videoSource: 'screen',
},
});
// publish support Promise way to catch errors
publish({
name: 'camera',
element: 'camera',
}).catch((ex) => {
console.log(ex);
});
According to the name you publish, you could use the same name to unpublish it:
const [opentokProps, opentokMethods] = useOpenTok();
const { unpublish } = opentokMethods;
// unpublish stream from the name 'camera'
unpublish({ name: 'camera' }
You can get all streams in the session through streams property in opentokProps. After finding the stream for subscribing, use the subscribe method to subscribe to the stream:
stream: the Stream Object wanted to subscribeelement: should be a DOM element or the id attribute of the existing DOM element.const [opentokProps, opentokMethods] = useOpenTok();
const { streams } = opentokProps;
const { subscribe } = opentokMethods;
const streamToSubscribe = streams[0];
subscribe({ stream: streamToSubscribe, element: 'subscriber' });
For each stream be subscribed, a subscriber object will be created and save as subscribers in opentokProps:
const [opentokProps, opentokMethods] = useOpenTok();
const { streams, subscribers } = opentokProps;
You can stop subscribing the stream with unsubscribe method:
const [opentokProps, opentokMethods] = useOpenTok();
const { unsubscribe } = opentokMethods;
const streamToUnsubscribe = streams[0];
unsubscribe({ stream: streamToUnsubscribe });
You can send signal in session with sendSignal method:
const [opentokProps, opentokMethods] = useOpenTok();
const { sendSignal } = opentokMethods;
sendSignal({
type: 'foo',
data: 'bar',
});
You could register all valid session events on session object. Take registering signal event, as an example, use the session object in opentokProps, register the session event with on, and unregister the event with off:
const [opentokProps, opentokMethods] = useOpenTok();
const { session } = opentokProps;
const handleSignal = useCallback(e => {
console.log('handleSignal', e);
}, []);
useEffect(() => {
if (!isSessionConnected) {
return;
}
session.on('signal', handleSignal);
return () => {
session.off('signal', handleSignal);
};
}, [handleSignal, isSessionConnected, session]);
NOTICE: for
sessionDisconnectedevent, you can yousession.once('sessionDisconnected', <eventHandler>)
$ npm install
$ npm start # rollup will watch the files change
$ npm build # rollup will build the package
$ npm test
$ cd site
$ npm install
$ npm start # start gatsby develop server
Thanks goes to these wonderful people (emoji key):
andyyou 💻 🎨 💡 🚧 🤔 👀 | PJCHENder 💻 📖 ⚠️ 💡 🚧 👀 | Maximillian Rink 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!
FAQs
React Hook for @opentok/client SDK
The npm package react-use-opentok receives a total of 19 weekly downloads. As such, react-use-opentok popularity was classified as not popular.
We found that react-use-opentok demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Season’s greetings from Socket, and here’s to a calm end of year: clean dependencies, boring pipelines, no surprises.

Research
/Security News
Impostor NuGet package Tracer.Fody.NLog typosquats Tracer.Fody and its author, using homoglyph tricks, and exfiltrates Stratis wallet JSON/passwords to a Russian IP address.

Security News
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.