Friday, August 29, 2014

WebRTC a high level look - Part II - RTCDataChannel


This is useful especially after the peer connection is established, to send the arbitrary data over the established peer connection. 
This is especially useful when there is something like game data to the transmitted to the other end. 

This is as easy as sending a json payload and use the send method to send it across. This is much similar to web socket. the below are few points about the RTCDataChannel 

- Same API as WebSockets 
- Ultra - low latency 
- Unreliable or reliable 
- Secure - Standard TLS encryption is used. 

The peerconection can be made for just audio video or include the peer data connection as well. 

A sample code is something like below 

var pc = new webKitRTCPeerConnection(servers, optional:[{RTCPChannels: true}])

pc.onDataChannel = function (event)
{
receiveDataChannel = event.channel; 
receiveChannel.onmessage = function (event)
{
document.querySelector ("div#receive").innerHTML = event.data;
};
};

sendChannel - pc.createDataChannel("sendDataChannel"m {reliable:true});

document.querySelector ("button#send").onClick = function ()
{
var data = document.querySelector("textarea#send").value;
sendChanenl.send(data);
};

On the Demo page it also show how the data can be transmitted peer to peer for e.g. a file. This doesn't even touch any of the servers. 

References:

No comments:

Post a Comment