title: WebRTC: Connecting multiple listeners to one client, one at a time
body: <p>I have been trying to get WebRTC to function with a broadcaster and multiple listeners but am stuck when it comes to transferal descriptions and candidates via signalling (with nodejs &amp; socket.io).</p>
  
  <p>I can get the process working between two browsers with a simple nodejs socket app which simply broadcasts the descriptions and candidates to other already connected clients, but when I attempt to store a description and connect with a newly opened browser, nothing happens.</p>
  
  <p>What I basically need to understand is what do I need to provide to one browser, in order for it to begin communicating with another? The project I am working on requires the ability for listeners to join rooms, authenticate, and begin listening to whatever media is being sent.</p>
  
  <p>Below is my client side code:</p>
  
  <pre><code>    var audioContext = new webkitAudioContext()
      var client = null
      var configuration = 
      {
          'iceServers': 
          [{
              'url': 'stun:stun.example.org'
          }]  
      }
  
      $(function ()
      {
          window.RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection
          client = new RTCPeerConnection(configuration, { optional:[ { RtpDataChannels: true } ]})
  
          client.onnegotiationneeded = function ()
          {
              console.log('Negotiation needed')
              createOffer()
          }
  
          client.onicecandidate = function (event)
          {
              console.log('onicecandidate')
              socket.emit('candidate', JSON.stringify({ 'candidate': event.candidate }))
          }
  
          client.onaddstream = function (event)
          {
              console.log('onaddstream')
              $('#player').attr('src', URL.createObjectURL(event.stream))
              player.play()
          }
  
          socket.on('candidate', function (event)
          {
              candidate(event)
          })
  
          socket.on('description', function (message)
          {
              if(!client) { return }
  
              client.setRemoteDescription(new RTCSessionDescription(message.sdp), function () {
  
              if (client.remoteDescription.type == 'offer')
                  client.createAnswer(function (description)
                  {
                      client.setLocalDescription(description, function ()
                      {
                          socket.emit('description', JSON.stringify({ 'sdp':client.localDescription }))
                      })
  
                  }, function (err)
                  {
                      console.log('error: ' + err)
                  })
  
              }, function(err)
              {
                  console.log('error: ' + err)
              })
          })
  
          addStream()
      })
  
      function createOffer ()
      {
          if(!client) { return; }
  
          client.createOffer(function (description)
          {
              console.log(description)
              client.setLocalDescription(description, function ()
              {
                  socket.emit('description', JSON.stringify({ 'sdp': client.localDescription }))
                  console.log('set local description')
              })
          })
      }
  
      function candidate (message)
      {
          if(message.candidate)
          {
              console.log('candidate')
              client.addIceCandidate(new RTCIceCandidate(message.candidate))
          }
      }
  
      function addStream ()
      {
          navigator.webkitGetUserMedia({audio: true, video: false}, function(stream)
          {
              client.addStream(stream)
          })
      }
  </code></pre>
  
  <p>And my signalling part of my server as it currently stands: </p>
  
  <pre><code>io.on 'connection', (socket) -&gt;
   socket.on 'description', (data) -&gt;
      parsed = JSON.parse data
      socket.broadcast.emit 'description', parsed
  
  socket.on 'candidate', (candidate) -&gt;
      parsed = JSON.parse candidate
      socket.broadcast.emit 'candidate', parsed
  </code></pre>
  
  <p>I'd appreciate any insight into this. Thanks.</p>