Add simple-peer configuration option. implements #8

This commit is contained in:
Kevin Jahns 2020-04-07 14:01:19 +02:00
parent 97df715a6b
commit 848976efe3
2 changed files with 37 additions and 4 deletions

View file

@ -58,6 +58,36 @@ const provider = new WebrtcProvider('your-room-name', ydoc, { maxConns: 70 + mat
Just listen to the "peers" event from the provider to listen for more incoming WebRTC connections and use the simple-peer API to share streams. More help on this would be welcome. By default, browser windows share data using BroadcastChannel without WebRTC. In order to connect all peers and browser windows with each other, set `maxConns = Number.POSITIVE_INFINITY` and `filterBcConns = true`.
## API
```js
new WebrtcProvider(roomName, ydoc[, opts])
```
The following default values of `opts` can be overwritten:
```js
{
// Specify signaling servers. The client will connect to every signaling server concurrently to find other peers as fast as possible.
signaling: ['wss://signaling.yjs.dev', 'wss://y-webrtc-signaling-eu.herokuapp.com', 'wss://y-webrtc-signaling-us.herokuapp.com'],
// If password is a string, it will be used to encrypt all communication over the signaling servers.
// No sensitive information (WebRTC connection info, shared data) will be shared over the signaling servers.
// The main objective is to prevent man-in-the-middle attacks and to allow you to securely use public / untrusted signaling instances.
password: null,
// Specify an existing Awareness instance - see https://github.com/yjs/y-protocols
awareness: new awarenessProtocol.Awareness(doc),
// Maximal number of WebRTC connections.
// A random factor is recommended, because it reduces the chance that n clients form a cluster.
maxConns: 20 + math.floor(random.rand() * 15),
// Whether to create WebRTC connections to other tabs in the same browser.
// Tabs within the same browser share document updates using BroadcastChannels.
// WebRTC connections within the same browser are therefore only necessary if you want to share video information too.
filterBcConns: true,
// simple-peer options. See https://github.com/feross/simple-peer#peer--new-peeropts for available options.
// y-webrtc uses simple-peer internally as a library to create WebRTC connections.
peerOpts: {}
}
```
### Logging

View file

@ -179,7 +179,7 @@ export class WebrtcConn {
/**
* @type {any}
*/
this.peer = new Peer({ initiator })
this.peer = new Peer({ initiator, ...room.provider.peerOpts })
this.peer.on('signal', signal => {
publishSignalingMessage(signalingConn, room, { to: remotePeerId, from: room.peerId, type: 'signal', signal })
})
@ -531,16 +531,18 @@ export class WebrtcProvider extends Observable {
* @param {awarenessProtocol.Awareness} [opts.awareness]
* @param {number} [opts.maxConns]
* @param {boolean} [opts.filterBcConns]
* @param {any} [opts.peerOpts]
*/
constructor (
roomName,
doc,
{
signaling = ['wss://signaling.yjs.dev', 'wss://y-webrtc-uchplqjsol.now.sh', 'wss://y-webrtc-signaling-eu.herokuapp.com', 'wss://y-webrtc-signaling-us.herokuapp.com'],
signaling = ['wss://signaling.yjs.dev', 'wss://y-webrtc-signaling-eu.herokuapp.com', 'wss://y-webrtc-signaling-us.herokuapp.com'],
password = null,
awareness = new awarenessProtocol.Awareness(doc),
maxConns = 20 + math.floor(random.rand() * 15), // just to prevent that exactly n clients form a cluster
filterBcConns = true
maxConns = 20 + math.floor(random.rand() * 15), // the random factor reduces the chance that n clients form a cluster
filterBcConns = true,
peerOpts = {} // simple-peer options. See https://github.com/feross/simple-peer#peer--new-peeropts
} = {}
) {
super()
@ -555,6 +557,7 @@ export class WebrtcProvider extends Observable {
this.signalingUrls = signaling
this.signalingConns = []
this.maxConns = maxConns
this.peerOpts = peerOpts
/**
* @type {PromiseLike<CryptoKey | null>}
*/