What is y-protocols?
The y-protocols npm package provides a set of protocols for building collaborative applications using Yjs, a high-performance CRDT (Conflict-free Replicated Data Type) framework. It includes protocols for awareness, WebRTC, and WebSocket communication, enabling real-time collaboration features such as shared editing and presence awareness.
What are y-protocols's main functionalities?
Awareness Protocol
The Awareness protocol allows you to manage and track the presence and state of users in a collaborative session. This can be used to show who is online and their current activity or status.
const { Awareness } = require('y-protocols/awareness');
const awareness = new Awareness(doc);
// Set user state
awareness.setLocalStateField('user', { name: 'Alice' });
// Listen for changes in awareness
awareness.on('change', changes => {
console.log('Awareness changed:', changes);
});
WebRTC Protocol
The WebRTC protocol enables peer-to-peer communication for real-time collaboration. It allows users to connect directly to each other without the need for a central server.
const { WebrtcProvider } = require('y-protocols/webrtc');
const provider = new WebrtcProvider('my-room-name', doc);
// Listen for connection events
provider.on('synced', () => {
console.log('Synced with WebRTC peers');
});
WebSocket Protocol
The WebSocket protocol facilitates real-time communication over WebSocket connections. This is useful for scenarios where a central server is required to manage connections and data synchronization.
const { WebsocketProvider } = require('y-protocols/websocket');
const provider = new WebsocketProvider('wss://my-websocket-server', 'my-room-name', doc);
// Listen for connection events
provider.on('status', event => {
console.log('WebSocket connection status:', event.status);
});
Other packages similar to y-protocols
automerge
Automerge is a library for building collaborative applications using CRDTs. It provides similar functionality to Yjs and y-protocols, allowing for real-time collaboration and conflict resolution. However, Automerge focuses more on simplicity and ease of use, while Yjs and y-protocols offer higher performance and more advanced features.
sharedb
ShareDB is a real-time database backend based on Operational Transformation (OT). It allows multiple users to collaborate on the same data in real-time. While ShareDB uses OT instead of CRDTs, it provides similar real-time collaboration features and can be used with WebSocket for communication.
gun
Gun is a decentralized, real-time, graph database that supports real-time data synchronization and offline-first capabilities. It provides similar real-time collaboration features as y-protocols but focuses on decentralized data storage and peer-to-peer communication.
Yjs Protocols
Binary encoding protocols for syncing, awareness, and history inforamtion
This API is unstable and subject to change.
API
Awareness Protocol
import * as awarenessProtocol from 'y-protocols/awareness.js'
The Awareness protocol implements a simple network agnostic algorithm that
manages user status (who is online?) and propagate awareness information like
cursor location, username, or email address. Each client can update its own
local state and listen to state changes of remote clients.
Each client has an awareness state. Remote awareness are stored in a Map that
maps from remote client id to remote awareness state. An awareness state is an
increasing clock attached to a schemaless json object.
Whenever the client changes its local state, it increases the clock and
propagates its own awareness state to all peers. When a client receives a remote
awareness state, and overwrites the clients state if the received state is newer
than the local awareness state for that client. If the state is null
, the
client is marked as offline. If a client doesn't receive updates from a remote
peer for 30 seconds, it marks the remote client as offline. Hence each client
must broadcast its own awareness state in a regular interval to make sure that
remote clients don't mark it as offline.
awarenessProtocol.Awareness Class
const awareness = new awarenessProtocol.Awareness()
clientID:number
- A unique identifier that identifies this client.
getLocalState():Object<string,any>|null
- Get the local awareness state.
setLocalState(Object<string,any>|null)
-
Set/Update the local awareness state. Set `null` to mark the local client as
offline.
setLocalStateField(string, any)
-
Only update a single field on the local awareness object. Does not do
anything if the local state is not set.
getStates():Map<number,Object<string,any>>
-
Get all client awareness states (remote and local). Maps from clientID to
awareness state.
on('change', ({ added: Array<number>, updated: Array<number>
removed: Array<number> }, [transactionOrigin:any]) => ..)
-
Listen to remote and local state changes on the awareness instance.
on('update', ({ added: Array<number>, updated: Array<number>
removed: Array<number> }, [transactionOrigin:any]) => ..)
-
Listen to remote and local awareness changes on the awareness instance.
This event is called even when the awarenes state does not change.
License
The MIT License © Kevin Jahns