How to broadcast video using RTCWeb APIs?

Copyright © 2013 Muaz Khan<@muazkh>.

If you're newcomer, newbie or beginner; you're suggested to try RTCMultiConnection.js or DataChannel.js libraries.
This document is considered obsolete. (Notification added at June 22, 2013).
If you're new to WebRTC; please read this document.
Old WebRTC video broadcasting experiment was splitted in 5 separate files.
  1. Helper.js - contains simple reusable code.
  2. RTC-Functions.js - contains 3 functions to send offer/answer SDP, send ICE, and get remote stream.
  3. ui.js - obviously user interface relevant code like create/join room buttons click event handlers and search private room events etc.
  4. Answer-Socket.js - the person who joins your room is the "answerer"...this file is for him!!
  5. Master-Socket.js - You, the master, who creates room. This file handles all broadcasting stuff!
Process was like this:
  1. Master socket creates a new room and starts broadcasting room over "main-public" channel for its lifetime.
    Note: "Main-Public" channel is a default channel that is opened for all on page load.
  2. If a person wants to join your room; he will send you join request over "Main-Public" channel.
  3. Master socket will "open a new socket" and use participant's token (unique ID) as "channel name". Participant will also open "same channel"; so now both can transfer SDP/ICE over that "private" channel.
  4. Now, it is newly created sockets job to exchange SDP/ICE with participant and share "same client stream" with him.
In simple words; there are more than one sockets opened (using the power of socket.io multiplexing). Also, more than one peers are created to share "stream" with unlimited participants.
In Master-Socket.js file; following code opens new socket for each participant:
function masterSocket(channel, onopen) {
    socket.master.on('connect', connect);
    socket.master.on('message', callback);
    function callback(data) {
        if (data.participant) {
            /* found a participant? .... open new socket for him */
And here is "openSocket" function:
function openSocket(channel) {
    socket.on('connect', opened);
    socket.on('message', callback);

    /* unique socket opened */
    function opened() {
        /* unique peer got video from participant */
        video = document.createElement('video');
        /* unique peer connection opened */
        peer = RTCPeerConnection(config);
    /* unique socket got message from participant */
    function callback(response) {
        /* answer sdp */
        if (response.firstPart || response.secondPart) { ... }

        /* ICE sent by your participant */
        if (response.candidate && !isGotRemoteStream) {
            peer && peer.addICE({
                sdpMLineIndex: response.candidate.sdpMLineIndex,
                candidate: JSON.parse(response.candidate.candidate)

    /* sub socket got stream */
    function gotstream(...) { ... }