YousefED / SyncedStore

SyncedStore CRDT is an easy-to-use library for building live, collaborative applications that sync automatically.
https://syncedstore.org
MIT License
1.71k stars 51 forks source link

Can I use SyncedStore solely between tabs in one browser? (No signaling servers) #34

Closed floer32 closed 2 years ago

floer32 commented 2 years ago

It seems like it's possible to use SyncedStore in multiple tabs in a browser, just locally. Just BroadcastChannel and y-indexeddb, no signaling servers, and I don't think it would need any WebRTC activity.

I can share some code I've tried but first I just wanted to ask the question... Is there an official way to use it purely locally?

floer32 commented 2 years ago

(I did try it and I couldn't get it working. So I'm wondering if there's just a known-good way, rather than the trial-and-error I've been doing.)

YousefED commented 2 years ago

Hi @hangtwenty.

Broadcastchannel (cross tabs) and WebRTC syncing can both be managed using y-webrtc.

I think it should be possible to disable the webrtc part in y-webrtc, and use only the broadcastchannel. Could you try to pass an empty array as signaling servers? e.g.:

new WebrtcProvider("roomname", doc, { signaling: [] })

(see https://github.com/yjs/y-webrtc/blob/c059b8679e72e4a2cd59189b022a25edbd07cbeb/src/y-webrtc.js#L545)

floer32 commented 2 years ago

@YousefED I tried something similar:

const doc = getYjsValue(store);
export const webrtcProvider = new WebrtcProvider("syncedstore-todos", doc as any, {
    signaling: [],
    filterBcConns: false,
    maxConns: Number.POSITIVE_INFINITY,
});
// @ts-ignore
const provider = new IndexeddbPersistence("syncedstore-todos", doc as any);

From the y-webrtc README it seemed like this part is needed to fully disable WebRTC connections:

filterBcConns: false

But I was experiencing inconsistent behavior and some errors.

I'll try another troubleshooting session tonight, and report back.

floer32 commented 2 years ago

I can't reproduce the issue now, for some reason. But it's working, so that's great!

YousefED commented 2 years ago

Glad to hear!