Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
	<title>WebRTC with SkylinkJS 2.x</title>
  
	<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
</head>
<body>

  <header>
    <a href="https://github.com/Temasys/SkylinkJS" target="_blank">SkylinkJS on Github</a>
    You're in a private room. Please click &quot;Start&quot;, allow access to your microphone and camera and <a href="https://codepen.io/temasys/pen/GogabE" target="_blank">click here</a> to open this page in a new tab. You're joining yourself for a fun call ;)
  </header>
  
  <p id="status">Loading room information...</p>
 
  <div id="start">
    <button  id="start_btn">Start</button><br/>
    <video id="myvideo" playsinline autoplay muted></video>
    <audio id="myaudio" playsinline autoplay muted></audio>
  </div>
  
</body>
</html>
              
            
!

CSS

              
                header {
  background: #eee;
  padding: 20px;
  font-family: Helvetica, Arial, sans-serif;
}

header a:first-child {
  float: right;
  margin: 0 0 20px 30px;
}

button {
  display: inline-block;
  position: relative;
  top: 110px;
  left: 110px;
  z-index: 10;
}

video {
  width: 267px;
  height: 200px;
  border: 1px solid white;
  outline: 1px solid #ccc;
  z-index: 5;
}

#myvideo {
  transform: rotateY(-180deg);
}

#start {
  display: none;
}
              
            
!

JS

              
                import Skylink, { SkylinkEventManager, SkylinkConstants, SkylinkEvents, SkylinkLogger } from "https://cdn.temasys.io/skylink/skylinkjs/2.4.0.1/skylink.complete.js"

const initOptions = {
  appKey: '474e3b65-6075-45b7-838b-6a211813cb7d', // Get your own key at https://console.temasys.io
  defaultRoom: 'temasys-codepen-2.x-safari'//getRoomId();
}

let skylink;

try {
  skylink = new Skylink(initOptions)
  SkylinkLogger.setLevel(SkylinkLogger.logLevels.TRACE);
  console.log(skylink.getSdkVersion());
  document.getElementById('status').innerHTML = 'Room information has been loaded. Room is ready for user to join.';
    document.getElementById('start').style.display = 'block';
} catch (err) {
  document.getElementById('status').innerHTML = 'Failed retrieval for room information.<br>Error: ' + (err.message || err.error);
}

SkylinkEventManager.addEventListener(SkylinkEvents.MEDIA_ACCESS_SUCCESS, (evt) => {
  console.log("MEDIA_ACCESS_SUCCESS", evt);
  let vid;
  let aud;
  const { isVideo, isAudio, stream } = evt.detail;
  if  (isVideo) {
    vid = document.getElementById('myvideo');
    attachMediaStream(vid, stream);
       setTimeout(() => {
    vid.controls = false;
  }, 1000);
  } else {
    aud = document.getElementById('myaudio');
    attachMediaStream(aud, stream);
       setTimeout(() => {
    aud.controls = false;
  }, 1000);
  }
})

SkylinkEventManager.addEventListener(SkylinkEvents.READY_STATE_CHANGE, (evt) => {
  console.log("READY_STATE_CHANGE", evt);
})

SkylinkEventManager.addEventListener(SkylinkEvents.PEER_JOINED, (evt) => {
  console.log("PEER_JOINED", evt);
  const { isSelf, peerId } = evt.detail;
  if(isSelf) return; // We already have a video element for our video and don't need to create a new one.
  const vid = document.createElement('video');
  vid.autoplay = true;
  vid.controls = true;
  vid.setAttribute('playsinline', true);
  vid.id = `${peerId}_video`;
  document.body.appendChild(vid);
  
  const aud = document.createElement('audio');
  aud.autoplay = true;
  aud.controls = true;
  aud.setAttribute('playsinline', true);
  aud.id = `${peerId}_audio`;
  document.body.appendChild(aud);
  
  setTimeout(() => {
    aud.controls = false;
    vid.controls = false;
  }, 1000);
})

SkylinkEventManager.addEventListener(SkylinkEvents.ON_INCOMING_STREAM, (evt) => {
  const { isSelf, peerId, stream, isAudio, isVideo } = evt.detail;
  if(isSelf) return;
  console.log("ON_INCOMING_STREAM", evt, evt.detail.stream.getTracks()[0]);
  if  (isVideo) {
      const vid = document.getElementById(`${peerId}_video`);
      attachMediaStream(vid, stream);
  } else if (isAudio) {
      const aud = document.getElementById(`${peerId}_audio`);
      attachMediaStream(aud, stream);
  } else {
    console.error("Stream kind not recognized");
  }
})

SkylinkEventManager.addEventListener(SkylinkEvents.PEER_LEFT, (evt) => {
  console.log("PEER_LEFT", evt);
  const { peerId } = evt.detail;
  const vid = document.getElementById(`${peerId}_video`);
  document.body.removeChild(vid);
  const aud = document.getElementById(`${peerId}_audio`);
  document.body.removeChild(aud);
})

document.getElementById("start_btn").addEventListener("click", () => {
  event.target.style.visibility = 'hidden';
  
  skylink.joinRoom({
    audio: true,
    video: true
  })
  .then((streams) => {
    document.getElementById('status').innerHTML = 'Joined room.';
  })
    .catch((err) => {
   document.getElementById('status').innerHTML = 'Failed joining room.<br>' +
  'Error: ' + (err.message || err.error);
  });
})

/* Helper functions */

function getRoomId() {
  var roomId = document.cookie.match(/roomId=([a-z0-9-]{36})/);
  if(roomId) {
    return roomId[1];
  }
  else {
    roomId = skylink.generateUUID();
    var date = new Date();
    date.setTime(date.getTime() + (30*24*60*60*1000));
    document.cookie = 'roomId=' + roomId + '; expires=' + date.toGMTString() + '; path=/';
    return roomId;
  }
};

              
            
!
999px

Console