Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create multiple DataChannels #694

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ addons:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
sauce_connect: true
sauce_connect:
no_ssl_bump_domains: airtap.local
hosts:
- airtap.local
env:
Expand Down
73 changes: 66 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ If `opts` is specified, then the default options (shown below) will be overridde
{
initiator: false,
channelConfig: {},
channelName: '<random string>',
channelName: 'default',
config: { iceServers: [{ urls: 'stun:stun.l.google.com:19302' }, { urls: 'stun:global.stun.twilio.com:3478?transport=udp' }] },
offerOptions: {},
answerOptions: {},
Expand All @@ -287,7 +287,7 @@ The options do the following:

- `initiator` - set to `true` if this is the initiating peer
- `channelConfig` - custom webrtc data channel configuration (used by [`createDataChannel`](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/createDataChannel))
- `channelName` - custom webrtc data channel name
- `channelName` - custom webrtc data channel name. Must be the same on both peers.
- `config` - custom webrtc configuration (used by [`RTCPeerConnection`](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection) constructor)
- `offerOptions` - custom offer options (used by [`createOffer`](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/createOffer) method)
- `answerOptions` - custom answer options (used by [`createAnswer`](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/createAnswer) method)
Expand Down Expand Up @@ -318,8 +318,8 @@ Send text/binary data to the remote peer. `data` can be any of several types: `S
`Buffer` (see [buffer](https://github.com/feross/buffer)), `ArrayBufferView` (`Uint8Array`,
etc.), `ArrayBuffer`, or `Blob` (in browsers that support it).

Note: If this method is called before the `peer.on('connect')` event has fired, then data
will be buffered.
Note: If you need to send data before `peer.on('connect')` event has fired, then use
`peer.write(data)` to buffer data.

### `peer.addStream(stream)`

Expand All @@ -345,13 +345,34 @@ Replace a `MediaStreamTrack` with another track. Must also pass the `MediaStream

Add a `RTCRtpTransceiver` to the connection. Can be used to add transceivers before adding tracks. Automatically called as neccesary by `addTrack`.

### `datachannel = peer.createDataChannel(channelName, channelConfig)`

Used to create additional `DataChannel` objects. `DataChannel`s are instances of `stream.Duplex`.

NOTE: Firefox currently [does not support](https://bugzilla.mozilla.org/show_bug.cgi?id=1513107) creating new datachannels after closing any datachannel.

### `peer.destroy([err])`

Destroy and cleanup this peer connection.

If the optional `err` parameter is passed, then it will be emitted as an `'error'`
event on the stream.

### `datachannel.send(data)`
Send text/binary data to the remote peer. Similar to `peer.send(data)`.

Note: If you need to send data before `datachannel.on('open')` event has fired, then use
`datachannel.write(data)` to buffer data.

### `datachannel.end()`
Closes and destroys the `DataChannel` after waiting for it to flush.

### `datachannel.close([err])`
Immediately closes and destroys the DataChannel without waiting for it to flush.

If the optional `err` parameter is passed, then it will be emitted as an `'error'`
event on the DataChannel.

### `Peer.WEBRTC_SUPPORT`

Detect native WebRTC support in the javascript environment.
Expand All @@ -368,7 +389,7 @@ if (Peer.WEBRTC_SUPPORT) {

### duplex stream

`Peer` objects are instances of `stream.Duplex`. They behave very similarly to a
`Peer` and `DataChannel` objects are instances of `stream.Duplex`. They behave very similarly to a
`net.Socket` from the node core `net` module. The duplex stream reads/writes to the data
channel.

Expand Down Expand Up @@ -409,7 +430,7 @@ Fired when the peer connection and data channel are ready to use.

### `peer.on('data', data => {})`

Received a message from the remote peer (via the data channel).
Received a message from the remote peer (via the default data channel).

`data` will be either a `String` or a `Buffer/Uint8Array` (see [buffer](https://github.com/feross/buffer)).

Expand All @@ -433,30 +454,68 @@ peer.on('stream', stream => {

Received a remote audio/video track. Streams may contain multiple tracks.

### `peer.on('datachannel', function (datachannel) {})`

Received an additional DataChannel. This fires after the remote peer calls `peer.createDataChannel()`. It will not fire for the default DataChannel.

### `peer.on('negotiate', () => {})`

The peer has completed a round of (re)negotiation, but may not be connected yet.

### `peer.on('close', () => {})`

Called when the peer connection has closed.
Fired when the peer connection has closed.

### `peer.on('error', (err) => {})`

Fired when a fatal error occurs. Usually, this means bad signaling data was received from the remote peer.

`err` is an `Error` object.

### `datachannel.on('open', () => {})`

Fired when the DataChannel has opened and is ready to use.

### `datachannel.on('data', () => {})`

Received a message from the remote peer (via the data channel).

`data` will be either a `String` or a `Buffer/Uint8Array` (see [buffer](https://github.com/feross/buffer)).

### `datachannel.on('close', () => {})`

Fired when the DataChannel has closed.

### `datachannel.on('error', (err) => {})`

Fired when a fatal error occurs on the DataChannel.

`err` is an `Error` object.

## error codes

Errors returned by the `error` event have an `err.code` property that will indicate the origin of the failure.

Possible error codes:
- `ERR_WEBRTC_SUPPORT`
- `ERR_PC_CONSTRUCTOR`
- `ERR_DESTROYED`
- `ERR_CREATE_OFFER`
- `ERR_CREATE_ANSWER`
- `ERR_SET_LOCAL_DESCRIPTION`
- `ERR_SET_REMOTE_DESCRIPTION`
- `ERR_ADD_ICE_CANDIDATE`
- `ERR_ADD_TRANSCEIVER`
- `ERR_SENDER_REMOVED`
- `ERR_SENDER_ALREADY_ADDED`
- `ERR_TRACK_NOT_ADDED`
- `ERR_REMOVE_TRACK`
- `ERR_UNSUPPORTED_REPLACETRACK`
- `ERR_ICE_CONNECTION_FAILURE`
- `ERR_ICE_CONNECTION_CLOSED`
- `ERR_SIGNALING`
- `ERR_DATA_CHANNEL`
- `ERR_INVALID_CHANNEL_NAME`
- `ERR_CONNECTION_FAILURE`


Expand Down
Loading