javascriptwebrtcopenwebrtc

How to know through which of the 2 channels the messages arrive [WebRTC]


I'm trying to identify which of the two channels a message came through, but I don't know how I can tell them apart.

How could I manage to do it?

let channel = null;
let channel2 = null;

channel = connection.createDataChannel('data');
channel2 = connection.createDataChannel('data2');


connection.ondatachannel = (event) => {

// I'm sure it's here, but I don't know how to tell the difference.
channel = event.channel;
channel2 = event.channel;


// Regardless of whether channel or channel2 is typed, the messages are mixed up.

channel.onmessage = (event) => {}
channel2.onmessage = (event) => {}


}


Solution

  • Datachannels have a 'label' property that you set to data and data2 respectively which gets signalled from the creator to the receiver. You can inspect event.channel.label and make decisions based on that.