Data Channels
Data channel is a powerful feature of WebRTC that allows you to exchange arbitrary data between peers in a room. In Peerix , you can open data channels to send messages, files, or any other type of data that does not fit into media streams.
sequenceDiagram
autonumber
Note over Peer1,Peer2: Open a data channel
Peer2->>Peer1: Is polite?<br>signaling DC: channel + options
Peer1->>Peer1: Is impolite?<br>Create the data channel
Peer1->>Peer2: channel event: new
Note over Peer1,Peer2: Channel is created
Opening Data Channels
You can open data channels to exchange arbitrary data with other peers in the room using the open method:
// listen for data channel open event
peer.on('channel:open', (e) => {
const { remote, label } = e;
console.log(
'Data channel opened with peer:', remote.id,
'channel label:', label
);
});
// open a data channel
peer.open({ label: 'chat' });Peerix allows you to open multiple data channels with different labels that should be unique, and you can also specify options for each channel, such as the ordered or unordered delivery of messages and other channel-specific settings.
Closing Data Channels
To stop exchanging data over a data channel, you can close it specifically by its label:
// listen for data channel close event
peer.on('channel:close', (e) => {
const { remote, label } = e;
console.log(
'Data channel closed with peer:', remote.id,
'channel label:', label
);
});
// close a data channel with a specific label
peer.close({ label: 'chat' });Note
You can open a data channel before or after connecting to a room, and the library will handle the sharing of the channel as needed with other peers in the room.
Sending and Receiving Messages
To send a message to all connected peers over open data channels, you can use the send method with the message and optionally a channel label:
// send a message to all connected peers and all open channels
peer.send('Hello, peers!');
// or with the channel label
peer.send('Hello, peers!', { label: 'chat' });To listen for incoming messages on a data channel, you can use the channel:message event:
// listen for incoming messages on a specific channel
peer.on('channel:message', (e) => {
const { remote, label, data } = e;
console.log(
'Message from peer:', remote.id,
'channel label:', label,
'data:', data
);
});Selective Operations
Peerix allows you to open, close, send messages and listen for events on specific peer connections, rather than on all of them in the room.
To send a message to a specific peer over a data channel, you can get the remote peer instance and use the send method with the message:
const remote = peer.connections.get('peer-id');
if (remote) {
remote.send('Hello, peer!', { label: 'chat' });
}The same way you can use the open and close methods on the remote peer instance and listen for events with the remote.on method to handle data channels with specific peers in the room.
Please note that these operations continue to run while the peer connection is established. If the connection is closed, you will need to open the data channels again to exchange data with that peer, even if they are reconnected later.