A demonstration of integrating Yjs (a CRDT library) with vis-network for real-time collaborative network graph editing. Multiple clients can simultaneously add, edit, and delete nodes and edges, with changes automatically synchronised via a WebSocket server.
The app uses a bidirectional sync pattern between local vis-network DataSets and Yjs shared maps:
- Local → Remote:
nodes.on('*')andedges.on('*')listeners detect local changes and updateyNodesMap/yEdgesMap. - Remote → Local:
yNodesMap.observe()andyEdgesMap.observe()detect remote changes and update the local DataSets.
Each node and edge is tagged with a clientID to prevent feedback loops — only changes from other clients trigger local updates.
A Disconnect/Connect button lets you toggle the WebSocket connection, demonstrating that edits made offline are merged when the connection is restored.
- Node.js (v16 or later recommended)
npm install # installs dependencies and runs the webpack build (via postinstall)To rebuild manually:
npm run dist # bundles y-vis-network.js → dist/y-vis-network.bundle.jsIn a separate terminal:
npx y-websocket-serverThis starts a Yjs WebSocket server on ws://localhost:1234.
Open y-vis-network.html in a browser (e.g. via a local HTTP server or directly as a file). Open the same page in a second browser window to see real-time synchronisation.
| File | Description |
|---|---|
y-vis-network.js |
Main application — sets up Yjs, vis-network, and the sync logic |
y-vis-network with random init.js |
Alternative version that initialises with random nodes |
y-vis-network.html |
Demo page |
y-vis-network.css |
Styling for the network container and node edit popup |
webpack.config.js |
Webpack configuration (bundles to dist/) |
The WebSocket server URL is set in y-vis-network.js (line 28). Alternative URLs are commented out:
ws://localhost:1234— default, local developmentwss://demos.yjs.dev— Yjs public demo server
MIT — see package.json for details.
Nigel Gilbert (n.gilbert@surrey.ac.uk)