Question

I'm trying to make a 2 persons video chat using OpenTok API, but I don't have a clue on how to generate a sessionId or a token and what's the difference between them.

I've looked into the provided examples, but they don't show how to generate them. So I would appreciate if someone could provide an example with explanations.

Was it helpful?

Solution

SessionIDs identify the video chat that you want to connect to. Many people can connect and publish video streams to the same session. You see and hear other people in a session based on which video streams your browser subscribes to in that session. Because your application controls who can publish and who subscribes to whom, you can create a wide variety of video chat topologies in your session (eg. 1:1, video conference, talk show, security cam, etc.).

Tokens are a security/authentication mechanism. When you initiate a connection to a given session, you must provide a token that was generated using the same credentials that created the session. Tokens prevent other sites from "party crashing" a session that you have created, if they manage to get their hands on your sessionId.

Furthermore, when you generate a token, you can imbue it with a role, which tells the OpenTok infrastructure what types of actions to allow. For instance, you can decide to give a particular connection moderation rights by initiating that connection with a token that has the moderator role.

OTHER TIPS

Here is a very basic example of how to generate the session ID and token:

<?php
    require_once 'SDK/API_Config.php';
    require_once 'SDK/OpenTokSDK.php';

    $apiObj = new OpenTokSDK(API_Config::API_KEY, API_Config::API_SECRET);

    $session = $apiObj->create_session($_SERVER["REMOTE_ADDR"]);

    $sessionId = $session->getSessionId();
    $token = $apiObj->generate_token($sessionId, "moderator");
?>

You can then drop these values into the JS code like this:

<script type="text/javascript">
    var apiKey = <?php echo API_Config::API_KEY; ?> ;
    var sessionId = "<?php echo $sessionId; ?>";
    var token = "<?php echo $token; ?>";

    var session;
    var publisher;
    var subscribers = {};

    session = TB.initSession(sessionId);

    //Video chat event listeners
    session.addEventListener('sessionConnected', sessionConnectedHandler);
    session.addEventListener('streamCreated', streamCreatedHandler);

    session.connect(apiKey, token);
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top