HenrikJoreteg / webrtc.js

WebRTC abstraction for managing it simple to manage multiple peer connections of various types.
http://simplewebrtc.com
158 stars 33 forks source link

Correct way to open an additional data channel? #13

Open lazd opened 10 years ago

lazd commented 10 years ago

I have the following code in place on two peers:

var webrtc = new SimpleWebRTC(/* ... */);
webrtc.on('peerStreamAdded', function(peer) {
  peer.getDataChannel('chat');
});

webrtc.on('channelOpen', function(channel) {
  if (channel.label === 'chat') {
    // This is triggered twice
    console.log('%s opened!', channel.label);
  }
});

The chat data channel actually opens twice because both peers open it at the same time. As a result, the data channel will actually just stop functioning after some time (without reporting that it has closed). If you examine what's going on, you can clearly see the data channel returned by peer.getDataChannel('chat') is not the same one being used by calls to webrtc.sendDirectlyToAll('chat', /*...*/).

How can I ensure the data channel is opened only once? I see the start method in peer.js opens the simplewebrtc data channel, and it works out to only happen once, but there's no hook to add additional data channels at that time.

lazd commented 10 years ago

A workaround, in the mean time, is to use the simplewebrtc data channel for all your data needs.

fippo commented 10 years ago

opening the simplewebrtc channel at that time is a workaround for the webrtc api which will only negotiate data channels if there was a data channel before the offer is created. The right thing to do is probably to set a "initiator" flag at the same time and only create data channels if you're the initiator. hm...

note that channel labels are not unique which breaks the assumption they're observable in the way it is currently done.