Add Video Chat in Php application using Webrtc/Opentok

pixabay.com

Sharing is caring!

226 Views -

In the current time when Physically meet someone is risky. In that case, Metting Virtually using Video Conferencing is a good and only option. Today we discuss how to Implement Video Chat in your Php based Web Application.

We can use Tokbox(now Vonage) OPENTOK API, Let’s start implementing. Tokbox(now Vonage) OPENTOK API, is a Paid service with one month Trial($10) free of use.

The Server

First Download Server SDK for Php -https://github.com/opentok/Opentok-PHP-SDK

Or If using Compose you can Install it using –

composer require opentok/opentok 4.4.x

Initialize it using Autoloader-

require "<projectpath>/vendor/autoload.php";

Initialize an OpenTok\OpenTok object with your own API Key and API Secret.

use OpenTok\OpenTok;

$opentok = new OpenTok($apiKey, $apiSecret);

Creating Sessions

use OpenTok\MediaMode;
use OpenTok\ArchiveMode;

// Create a session that attempts to use peer-to-peer streaming:
$session = $opentok->createSession();

// A session that uses the OpenTok Media Router, which is required for archiving:
$session = $opentok->createSession(array( 'mediaMode' => MediaMode::ROUTED ));

// A session with a location hint:
$session = $opentok->createSession(array( 'location' => '12.34.56.78' ));

// An automatically archived session:
$sessionOptions = array(
    'archiveMode' => ArchiveMode::ALWAYS,
    'mediaMode' => MediaMode::ROUTED
);
$session = $opentok->createSession($sessionOptions);

// Store this sessionId in the database for later use
$sessionId = $session->getSessionId();

Generating Tokens

Once a Session is created, you can start generating Tokens for users to use for connecting to a created session.

use OpenTok\Session;
use OpenTok\Role;

// Generate a Token from just a sessionId (fetched from a database)
$token = $opentok->generateToken($sessionId);
// Generate a Token by calling the method on the Session (returned from createSession)
$token = $session->generateToken();

// Set some options in a token
$token = $session->generateToken(array(
    'role'       => Role::MODERATOR,
    'expireTime' => time()+(7 * 24 * 60 * 60), // in one week
    'data'       => 'name=Johnny',
    'initialLayoutClassList' => array('focus')
));

The Client

Loading OpenTok.js
To load OpenTok.js in your web page, add the following script tag:

<script src="https://static.opentok.com/v2/js/opentok.min.js"></script>

Initializing a Session object

// Replace with your OpenTok API key and session ID:
var session = OT.initSession(apiKey, sessionID);

Publishing audio or video only

var pubOptions = {publishAudio:true, publishVideo:false};

// Replace replacementElementId with the ID of the DOM element to replace:
publisher = OT.initPublisher(replacementElementId, pubOptions);

Subscribing to audio or video only

var options = {subscribeToAudio:true, subscribeToVideo:false};

// Replace stream and replacementElementId with your own values:
subscriber = session.subscribe(stream, replacementElementId, options);

Check more at official documentation – https://tokbox.com/developer/guides/connect-session/js/

Conclusion
This tutorial helped us how to implement OPENTOK API to develop a starter Video Conference application.

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments