Skip to content

Instantly share code, notes, and snippets.

@ehatricksmith
Created November 30, 2022 03:51
Show Gist options
  • Save ehatricksmith/683bde4718e1c99b7dc76534ac79407e to your computer and use it in GitHub Desktop.
Save ehatricksmith/683bde4718e1c99b7dc76534ac79407e to your computer and use it in GitHub Desktop.
import * as Y from "yjs";
import { WebsocketProvider } from "y-websocket";
const doc = new Y.Doc();
// The provider takes care of broadcasting and receiving sync and awareness.
// This will automatically perform the initial sync.
const provider = new WebsocketProvider(
"wss://dovetailapp.com/api/yjs",
"00000000-0000-0000-0000-000000000000", // docId
doc
);
// Create a top level data type
const yarray = ydoc.getArray("myList");
// Create a new edit
yarray.insert(0, [1]);
// `provider` will automatically send the change to the server
// Listen to changes to this part of the document from any client
yarray.observe(changeEvent => {
// See what has changed
changeEvent.changes.added; // Set<Y.Item>
changeEvent.changes.deleted; // Set<Y.Item>
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment