I am trying to create a file sharing site using WebRTC.
My testing environment:
Chrome Canary 42.0.2275.0 Mac 10.10 (14A389)
Code:
var arrayToStoreChunks = [];
channel.onmessage = function(data){
// console.log(data.data);
var data = data.data;
arrayToStoreChunks.push(data.message); // pushing chunks in array
if (data.last) {
downloadURI(arrayToStoreChunks.join(''), 'some.jpg');
arrayToStoreChunks = []; // resetting array
}
};
var button = document.getElementById('button');
var fileElement = document.getElementById('files');
button.onclick = function(){
var reader = new FileReader()
reader.readAsDataURL(fileElement.files[0])
reader.onload = onReadAsDataURL;
};
function downloadURI(uri, name) {
var link = document.createElement("a");
link.download = name;
link.href = uri;
link.click();
}
var chunkLength = 1000;
function onReadAsDataURL(event, text) {
var data = {}; // data object to transmit over data channel
if (event) {
text = event.target.result;
}
if (text.length > chunkLength) {
data.message = text.slice(0, chunkLength);
} else {
data.message = text;
data.last = true;
}
channel.send(data);
var remainingDataURL = text.slice(data.message.length);
if (remainingDataURL.length) {
setTimeout(function () {
onReadAsDataURL(null, remainingDataURL); // continue transmitting
}, 500);
}
}
I am chunking the data and sending at whatever size per message with whatever delay I specify. This works when I have the var optionalRtpDataChannels = {optional: [{'RtpDataChannels': true}]};
and pass that object into new RTCPeerConnection(servers, optionalRtpDataChannels);
. However, this is too slow for a file sharing web app and I would like to use WebRTC DataChannels to do this.
I tried what was suggested on this SO answer Send image data over RTC Data Channel with no luck. It suggests that if I leave out the RtpDataChannels: true
option that data.send() will be throttled to 64KB/s as opposed to 3KB/s(confirmed by my testing) which is what RTP is throttled to.
Any ideas on how I can send larger files over a DataChannel faster than 3KB/s?