From 848976efe3c4e3ea1afb644efec83f954a0d71df Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Tue, 7 Apr 2020 14:01:19 +0200 Subject: [PATCH] Add simple-peer configuration option. implements #8 --- README.md | 30 ++++++++++++++++++++++++++++++ src/y-webrtc.js | 11 +++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ff430e4..75fda66 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/y-webrtc.js b/src/y-webrtc.js index a575afe..45fa40f 100644 --- a/src/y-webrtc.js +++ b/src/y-webrtc.js @@ -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} */