Search code examples
javascriptopentoktokbox

OpenTok incoming video streams getting attached to the bottom of the body


Incoming video streams are getting attached at the bottom of the body. In my html I have the two divs:

<div id="myPublisherDiv" style="width:320px; height:240px; background-color:#ffffff"></div>
<div id="remoteVideo" style="width:320px; height:240px; background-color:#ffffff"></div>

Here is my javascript:

var remoteVideo = document.getElementById('remoteVideo');
var apiKey = "xxxx";
var sessionId = "xxxx";
var token = "xxxx";
var publisher = TB.initPublisher(apiKey, 'myPublisherDiv');
var session = TB.initSession(sessionId);

session.addEventListener('sessionConnected', function(e){
   session.publish( publisher );
   for (var i = 0; i < e.streams.length; i++) {
      if (e.streams[i].connection.connectionId == session.connection.connectionId) {
        return;
      }
      var div = document.createElement('div');
      div.setAttribute('id', 'stream' + e.streams[i].streamId);
      remoteVideo.appendChild(div);
      session.subscribe(e.streams[i]);
   }
});

session.addEventListener('streamCreated', function(e){
   for (var i = 0; i < e.streams.length; i++) {
      if (e.streams[i].connection.connectionId == session.connection.connectionId) {
         return;
      }
      var div = document.createElement('div');
      div.setAttribute('id', 'stream' + e.streams[i].streamId);
      remoteVideo.appendChild(div);
      session.subscribe(e.streams[i], div.id);
   }
});

Solution

  • In your sessionConnected event handler, you need to pass in the div id to the subscribe function. (Documentation)

    session.subscribe(e.streams[i], div.id);