Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create data channel in WebRTC peer connection?

I'm trying to learn how to create an RTCPeerConnection so that I can use the DataChannel API. Here's what I have tried from what I understood:

var client = new mozRTCPeerConnection;
var server = new mozRTCPeerConnection;

client.createOffer(function (description) {
    client.setLocalDescription(description);
    server.setRemoteDescription(description);

    server.createAnswer(function (description) {
        server.setLocalDescription(description);
        client.setRemoteDescription(description);

        var clientChannel = client.createDataChannel("chat");
        var serverChannel = server.createDataChannel("chat");

        clientChannel.onmessage = serverChannel.onmessage = onmessage;

        clientChannel.send("Hello Server!");
        serverChannel.send("Hello Client!");

        function onmessage(event) {
            alert(event.data);
        }
    });
});

I'm not sure what's going wrong, but I'm assuming that the connection is never established because no messages are being displayed.

Where do I learn more about this? I've already read the Getting Started with WebRTC - HTML5 Rocks tutorial.

like image 860
Aadit M Shah Avatar asked Jan 03 '13 06:01

Aadit M Shah


People also ask

What is data channel in WebRTC?

What is a data channel? A WebRTC data channel lets you send text or binary data over an active connection to a peer. In the context of a game, this lets players send data to each other, whether text chat or game status information.

What is a data channel?

A data channel is a computer path used when transferring data from one device to another.

Does WebRTC use Sctp?

WebRTC uses the Stream Control Transmission Protocol (SCTP), defined in RFC 4960. SCTP is a transport layer protocol that was intended as an alternative to TCP or UDP. For WebRTC we use it as an application layer protocol which runs over our DTLS connection.


2 Answers

I finally got it to work after sifting through a lot of articles: http://jsfiddle.net/LcQzV/

First we create the peer connections:

var media = {};
media.fake = media.audio = true;
var client = new mozRTCPeerConnection;
var server = new mozRTCPeerConnection;

When the client connects to the server it must open a data channel:

client.onconnection = function () {
    var channel = client.createDataChannel("chat", {});

    channel.onmessage = function (event) {
        alert("Server: " + event.data);
    };

    channel.onopen = function () {
        channel.send("Hello Server!");
    };
};

When the client creates a data channel the server may respond:

server.ondatachannel = function (channel) {
    channel.onmessage = function (event) {
        alert("Client: " + event.data);
    };

    channel.onopen = function () {
        channel.send("Hello Client!");
    };
};

We need to add a fake audio stream to the client and the server to establish a connection:

navigator.mozGetUserMedia(media, callback, errback);

function callback(fakeAudio) {
    server.addStream(fakeAudio);
    client.addStream(fakeAudio);
    client.createOffer(offer);
}

function errback(error) {
    alert(error);
}

The client creates an offer:

function offer(description) {
    client.setLocalDescription(description, function () {
        server.setRemoteDescription(description, function () {
            server.createAnswer(answer);
        });
    });
}

The server accepts the offer and establishes a connection:

function answer(description) {
    server.setLocalDescription(description, function () {
        client.setRemoteDescription(description, function () {
            var port1 = Date.now();
            var port2 = port1 + 1;

            client.connectDataConnection(port1, port2);
            server.connectDataConnection(port2, port1);
        });
    });
}

Phew. That took a while to understand.

like image 80
Aadit M Shah Avatar answered Oct 06 '22 19:10

Aadit M Shah


I've posted a gist that shows setting up a data connection, compatible with both Chrome and Firefox.

The main difference is that where in FF you have to wait until the connection is set up, in Chrome it's just the opposite: it seems you need to create the data connection before any offers are sent back/forth:

var pc1 = new RTCPeerConnection(cfg, con);
if (!pc1.connectDataConnection) setupDC1();    // Chrome...Firefox defers per other answer

The other difference is that Chrome passes an event object to .ondatachannel whereas FF passes just a raw channel:

pc2.ondatachannel = function (e) {
    var datachannel = e.channel || e;

Note that you currently need Chrome Nightly started with --enable-data-channels for it to work as well.

like image 26
natevw Avatar answered Oct 06 '22 17:10

natevw