socket.io
socket.io copied to clipboard
High latency in broadcast.emit
I was trying to kind of stream a video, by using canvas drawimage and sending it over using socket.io. However, I am getting a lag of around 1s in this method. I get lagless video, when I use socket.emit, but using io,sockets or socket.broadcast gives lag. Here's my code for socket.js:
var server = require('http').createServer().listen(1337, function(){console.log('Socket Server on 1337..');});
var io = require('socket.io').listen(server);
var debug = require('debug');
var time = new Date().getTime();
io.on('connection', function(socket){
console.log('Socket Connection Established');
socket.on('video-data', function(data){
socket.broadcast.emit('video-data', data);
});
});
Here's my client javascript (Snippets):
$(document).ready(function(){
io_connect();
initiateVideo();
}
function io_connect(){
socket = io('http://localhost:1337');
socket.on('connection', function(data){
//Something
});
socket.emit('user-connect', {"user":user});
socket.on('user-connect', function(data){
if(data.user != user){
users.push(data.user);
}
});
socket.on('video-data', displayVideo);
}
function displayVideo(data){
if(data.user != user){
var url = window.URL.createObjectURL(dataURIToBlob(data.data.toString()));
var img = new Image();
img.onload = function(){
document.getElementById("videoOthers").getContext('2d').drawImage(img, 0, 0);
}
img.src = url;
}
}
function initiateVideo(){
var cnvs = document.getElementById("videoSelf");
var ctx = cnvs.getContext("2d");
cnvs.width = document.getElementsByTagName("video")[0].offsetWidth;
cnvs.height = document.getElementsByTagName("video")[0].offsetHeight;
var cnvs2 = document.getElementById("videoOthers");
var ctx2 = cnvs.getContext("2d");
cnvs2.width = cnvs.width;
cnvs2.height = cnvs.height;
cnvs2.style.top = "-"+document.getElementById("videoSelf").offsetHeight+"px";
cnvs2.style.left = document.getElementById("videoSelf").offsetWidth+"px";
if(!hasGetUserMedia()){alert("No getUserMedia support. Kindly update your browser or use latest version of Chrome.");}
var errorCallback = function(e){console.log("Error", e);};
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
var boardConstraints = {
video: {
mandatory: {
maxWidth: document.getElementsByTagName('video')[0].offsetWidth,
maxHeight: document.getElementsByTagName('video')[0].offsetHeight
}
}, audio: false
};
if(navigator.getUserMedia){
var video = document.querySelector('video');
navigator.getUserMedia(boardConstraints, function (stream) {
video.src = window.URL.createObjectURL(stream);
}, errorCallback);
var timer = setInterval(function(){
ctx.drawImage(video, 0, 0, cnvs.width, cnvs.height);
document.getElementsByTagName('video')[0].style.display="none";
if(video_on){
var data = cnvs.toDataURL('image/jpeg', 0.8);
socket.emit('video-data', {"user":user,"data":data});
}
}, 40);
}
}
//Got from stackoverflow
function dataURIToBlob(dataURI){
//convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if(dataURI.split(',')[0].indexOf('base64') >= 0){
byteString = atob(dataURI.split(',')[1]);
}
else{
byteString = unescape(dataURI.split(',')[1]);
}
//separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(',')[0];
//write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for(var i = 0; i < byteString.length; i++){
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type:mimeString});
}
And here's the part of index.jade where video is streamed:
#videoBox
video(autoplay)
canvas#videoSelf
canvas#videoOthers
Any idea, where I am wrong?
That issue was closed automatically. Please check if your issue is fixed with the latest release, and reopen if needed (with a fiddle reproducing the issue if possible).