Skip to content

Instantly share code, notes, and snippets.

@ehatricksmith
Created November 30, 2022 03:54
Show Gist options
  • Save ehatricksmith/85ff2ae3023f8fc75b1644347889513b to your computer and use it in GitHub Desktop.
Save ehatricksmith/85ff2ae3023f8fc75b1644347889513b 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
const provider = new WebsocketProvider(
"wss://dovetailapp.com/api/yjs",
"00000000-0000-0000-0000-000000000000", // docId
doc
);
// Listens for awareness changes from other clients
provider.awareness.on('change', changes => {
// The new awareness state changes
changes;
// Complete map of awareness states, in the form of `clientId => JSON`
provider.awareness.getStates();
})
// Sets the awareness state of the current client, which provider will send to the server
provider.awareness.setLocalState({
userId: "00000000-0000-0000-0000-000000000000",
cursorPosition: { x: 100, y: 200 },
selectedNoteIds: [
"00000000-0000-0000-0000-000000000000",
"11111111-1111-1111-1111-111111111111",
"22222222-2222-2222-2222-222222222222",
],
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment