Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  
  <div id="status"></div>
  <button onClick="toggle(!isPublishing)">Toggle Publish</button>
  <div id="publisher"></div>
  
  <script src="https://static.opentok.com/v2/js/opentok.js"></script>
  
  <script>
    OT.setLogLevel(OT.DEBUG);
    
    const $status = document.getElementById('status');
    
    const apiKey = 'YOURAPIKEY';
    const sessionId = 'YOURSESSIONID';
    const token = 'YOURTOKEN';
    
    const pub = OT.initPublisher('publisher');
    pub.on('streamDestroyed', (e) => {
      e.preventDefault();
      $status.textContent = 'Unpublished';
    });
    let isPublishing = false;
    $status.textContent = 'Unpublished';
    
    const sess = OT.initSession(apiKey, sessionId);
    sess.connect(token, (err) => {
      if (err) {
        console.error('Failed to connect', err);
        return;
      }
      
      toggle(true);
    });
    
    function toggle(shouldPublish) {
      if (isPublishing === shouldPublish) {
        return;
      }
      isPublishing = shouldPublish;
      if (isPublishing) {
        $status.textContent = 'Publishing...';
        sess.publish(pub, (err) => {
          if (err) {
            console.error('Failed to publish', err);
            $status.textContent = 'Failed to publish';
          } else {
            $status.textContent = 'Published';
          }
        });
      } else {
        sess.unpublish(pub);
        $status.textContent = 'Unpublishing...';
      }
    }
  </script>
</body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
aihampro
0viewers