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

Add web rtc options #25

Open
wants to merge 1 commit into
base: main
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
9 changes: 3 additions & 6 deletions packages/matrix-crdt/src/MatrixProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import {
MatrixCRDTEventTranslator,
MatrixCRDTEventTranslatorOptions,
} from "./MatrixCRDTEventTranslator";
import { WebRTCOptions } from "./webrtc/WebrtcProvider";

const DEFAULT_OPTIONS = {
enableExperimentalWebrtcSync: false,
experimentalWebrtcPeers: [] as string[],
reader: {} as MatrixReaderOptions,
writer: {} as ThrottledMatrixWriterOptions,
translator: {} as MatrixCRDTEventTranslatorOptions,
Expand Down Expand Up @@ -265,15 +267,10 @@ export class MatrixProvider extends lifecycle.Disposable {
* TODO: we should probably extract this from MatrixProvider so that
* API consumers can instantiate / configure this seperately
*/
private async initializeWebrtc() {
private async initializeWebrtc(options?: WebRTCOptions) {
if (!this._roomId) {
throw new Error("not initialized");
}
/*
TODO:
- implement password
- allow options to be passed to WebRtcprovider (e.g.: signalling servers which now default to those supplied by yjs)
*/
if (!this.reader) {
throw new Error("needs reader to initialize webrtc");
}
Expand Down
40 changes: 40 additions & 0 deletions packages/matrix-crdt/src/webrtc/DocWebrtcProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @vitest-environment jsdom
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise it says it can't find WebSockets

*/

import { describe, expect, it } from "vitest";
import { DocWebrtcProvider } from "./DocWebrtcProvider";
import { WebRTCOptions } from "./WebrtcProvider";
import { Doc } from "yjs";
import { createRandomMatrixClientAndRoom } from "../test-utils/matrixTestUtil";
import { HOMESERVER_NAME } from "../test-utils/matrixTestUtilServer";

describe("WebrtcProvider", () => {
it("should pass the right options to the provider", async () => {
const { roomName } = await createRandomMatrixClientAndRoom(
"public-read-write"
);
const alias = "#" + roomName + ":" + HOMESERVER_NAME;
const withDefaults = new DocWebrtcProvider(alias, new Doc());
//@ts-expect-error // private
expect(withDefaults.signalingConns[0].url).toEqual(
"wss://signaling.yjs.dev"
);
expect(withDefaults.filterBcConns).toEqual(true);
expect(withDefaults.maxConns).toBeGreaterThan(20); //default is 20 + math.floor(random.rand() * 15);

const localServer = "ws://localhost:4444";
const options: WebRTCOptions = {
password: "password",
signaling: [localServer],
maxConns: 10,
filterBcConns: false,
};
const doc = new Doc();
const provider = new DocWebrtcProvider(alias, doc, options);
//@ts-expect-error // private
expect(provider.signalingConns[0].url).toEqual(localServer);
expect(provider.filterBcConns).toEqual(false);
expect(provider.maxConns).toEqual(10);
});
});
4 changes: 2 additions & 2 deletions packages/matrix-crdt/src/webrtc/DocWebrtcProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as awarenessProtocol from "y-protocols/awareness";
import * as syncProtocol from "y-protocols/sync";
import * as Y from "yjs"; // eslint-disable-line
import { globalRooms } from "./globalResources";
import { WebrtcProvider } from "./WebrtcProvider";
import { WebRTCOptions, WebrtcProvider } from "./WebrtcProvider";
import * as logging from "lib0/logging";

const log = logging.createModuleLogger("y-webrtc");
Expand Down Expand Up @@ -139,7 +139,7 @@ export class DocWebrtcProvider extends WebrtcProvider {
constructor(
roomName: string,
private readonly doc: Y.Doc,
opts?: any,
opts?: WebRTCOptions,
public readonly awareness = new awarenessProtocol.Awareness(doc)
) {
super(roomName, opts);
Expand Down
14 changes: 11 additions & 3 deletions packages/matrix-crdt/src/webrtc/WebrtcProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ const openRoom = (
globalRooms.set(name, room);
return room;
};
export interface WebRTCOptions {
signaling?: string[];
password?: string;
maxConns?: number;
filterBcConns?: boolean;
/** simple-peer options. See https://github.com/feross/simple-peer#peer--new-peeropts */
peerOpts?: any;
}

export abstract class WebrtcProvider extends Observable<string> {
// public readonly awareness: awarenessProtocol.Awareness;
Expand Down Expand Up @@ -59,12 +67,12 @@ export abstract class WebrtcProvider extends Observable<string> {
"wss://y-webrtc-signaling-eu.herokuapp.com",
"wss://y-webrtc-signaling-us.herokuapp.com",
],
password = undefined as undefined | string,
password,
// awareness = new awarenessProtocol.Awareness(doc),
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
} = {}
peerOpts = {},
}: WebRTCOptions = {}
) {
super();
this.filterBcConns = filterBcConns;
Expand Down