Connecting Multiple Data Channels from One Peer Connection

1,637 views
Skip to first unread message

Wei

unread,
Apr 23, 2017, 6:35:35 PM4/23/17
to discuss-webrtc

Hi All:


There are two peers: peer-1 and peer-2. They both declared four variables like the following:

           var dataSendChannelA;

           var dataSendChannelB;

           var dataReceiveChannelA;

           var dataReceiveChannelB;


They both created two data channels from one peer connection like the following:

dataSendChannelA = peerConnection.createDataChannel(“channelA”, {reliable: false};

dataSendChannelB = peerConnection.createDataChannel(“channelB”, {reliable: false};

 

When peer-1 is ready to connect to the peer-2's data channels, I only know the following:

peerConnection.ondatachannel = function (event) { 

  dataReceiveChannel = event.channel;


What I do not know is how to get both dataChannelA and dataChannelB connected with the corresponding peer-2's channels.  I am guessing that the “event” object should have the channel ID/label information, but I do not know how to get to it.

 

Help is greatly appreciated.

 

-wei

 

 

Taylor Brandstetter

unread,
Apr 24, 2017, 2:43:08 PM4/24/17
to discuss-webrtc
The data channel has a "label" attribute you can read. Or you can just negotiate the channels out-of-band, assigning IDs directly such that they match up on each side:

dataChannelA = peerConnection.createDataChannel(“channelA”, {negotiated: true, id: 1};
dataChannelB = peerConnection.createDataChannel(“channelB”, {negotiated: true, id: 2};

Then you won't even need to use the "datachannel" event. You can just wait for the readyState to reach "open".

By the way, "reliable: false" will not have any effect. If you want an unreliable channel, you must use either "maxRetransmitTime" (renamed to "maxPacketLifeTime" in the spec, but not yet in Chrome) or "maxRetransmits".

--

---
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrtc+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/df5bc883-308e-42f9-bd64-aa0aba98cf08%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wei

unread,
Apr 24, 2017, 4:36:25 PM4/24/17
to discuss-webrtc
Taylor,

Thank you very much for the pointer. If I use "negotiated" as you suggested in peer-1,  how would I handle the auto-negotiation and readyState === "open" in peer-2?  How are the auto-negotiation and  testing readyState related to peer Connection's "ondatachannel" event? Would appreciate a sample code snippet.

-wei,
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrt...@googlegroups.com.

Taylor Brandstetter

unread,
Apr 24, 2017, 5:03:25 PM4/24/17
to discuss-webrtc
To use "negotiated", you would just call "createDataChannel" on both peers, with the same arguments.

ID negotiated in-band:

// On peer 1:
channel = pc.createDataChannel("label");

// On peer 2:
pc.ondatachannel = function (event) { 
  channel = event.channel;
}

ID negotiated out-of-band:

// On peer 1:
channel = pc.createDataChannel("label", {negotiated: true, id: 1});

// On peer 2:
channel = pc.createDataChannel("label", {negotiated: true, id: 1});


My point about waiting for "readyState" to be "open" is just that you can't send data until it is. That applies in both cases.


To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrtc+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/015d82bf-3bcb-4bf2-b43d-f26a141a7991%40googlegroups.com.

Wei

unread,
Apr 24, 2017, 6:59:42 PM4/24/17
to discuss-webrtc
Taylor,

Thank you very much.  You have been a great help.

-wei,
Reply all
Reply to author
Forward
0 new messages