Back to Plate

Collaboration Demo

content/docs/examples/collaboration-example.mdx

53.0.82.0 KB
Original Source

This example shows a Plate editor bound to a shared Yjs document. It uses the collaboration-demo registry example with YjsPlugin, Hocuspocus, WebRTC, room state, connection controls, and remote cursor rendering.

Demo

<ComponentPreview name="collaboration-demo" />

Source

The demo creates a room id in localStorage, gives each tab a random user name and cursor color, then initializes Yjs after mount.

<ComponentSource name="collaboration-demo" />

Runtime Shape

PieceOwnerNotes
YjsPlugin@platejs/yjs/reactBinds Plate to a shared Y.Doc, provider list, awareness data, and cursor data.
Hocuspocus providerApp infrastructureThe demo points to ws://localhost:8888; run your own server for durable collaboration.
WebRTC providery-webrtcThe demo uses ws://localhost:4444 in dev and wss://signaling.yjs.dev in production.
Room stateRegistry exampleThe room id is stored in localStorage and passed to every provider plus yjs.init.
Connection controlsRegistry exampleThe status bar reads _providers and _isConnected, then calls connect or disconnect.

Set skipInitialization: true on the editor. Yjs owns the initial document load, and editor.getApi(YjsPlugin).yjs.init() seeds the value only when the shared document is empty.

Remote Cursors

The demo renders remote selections with the registry remote-cursor-overlay component.

<ComponentSource name="remote-cursor-overlay" />

RemoteCursorOverlay waits for _isSynced, reads cursor positions with useRemoteCursorOverlayPositions, then paints selection rectangles and a named caret.

Try It

Open the same demo in two tabs and use the same room id. Each tab receives a different random user name and cursor color, so typing and selections are easy to distinguish.

  • Yjs covers provider setup, lifecycle methods, and API reference.
  • Remote Cursor Overlay covers the reusable cursor UI component.