yjs
module. It is defined in y-protocols and is usually implemented by the providers. If you want to implement your custom provider, you can use the Awareness CRDT, or implement a custom protocol. It's up to you.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.awareness = new awarenessProtocol.Awareness(ydoc: Y.Doc)
Create a new awareness instance.awareness.clientID: number
A unique identifier that identifies this client.awareness.destroy()
Destroy the awareness instance and all associated state and event-handlers.awareness.getLocalState(): Object<string, any> | null
Get the local awareness state.awareness.setLocalState(state: Object<string, any>)
Set or update the local awareness state. Set null
to mark the local client as offline. The state
object must be a key-value store that maps to JSON-encodable values.awareness.setLocalStateField(string, any)
Only set or update a single key-value pair in the local awareness state.awareness.getStates(): Map<string, Object<string, any>>
Get all awareness states (remote and local). Maps from clientID
to awareness state. The clientID is usually the ydoc.clientID
.awareness.on('update', ({ added: Array, updated: Array removed: Array }, [transactionOrigin:any]) => ..)
Listen to remote and local awareness changes. This event is called even when the awareness state does not change but is only updated to notify other users that this client is still online. Use this event if you want to propagate awareness state to other users.awareness.on('change', ({ added: Array, updated: Array removed: Array }, [transactionOrigin:any]) => ..)
Listen to remote and local state changes. Get notified when a state is either added, updated, or removed.awarenessProtocol.encodeAwarenessUpdate(awareness: Awareness, clients: Array<number>): Uint8Array
Encode the awareness states of the specified clients into an update encoded as Uint8Array
.awarenessProtocol.applyAwarenessUpdate(awareness: Awareness, update: Uint8array)
Apply an awareness update created with encodeAwarenessUpdate
to an instance of the Awareness CRDT.awarenessProtocol.removeAwarenessStates(awareness: Awareness, clients: Array<number>, origin: any)
Remove the awareness states of the specified clients. This will call the update
and the change
event handler of the Awareness CRDT. Sometimes you want to mark yourself or others as offline. As soon as you know that a client is offline, you should call this function. It is not part of the Awareness CRDT, because it should only be used by the provider that implements awareness.update
event. The only difference is that the Awareness CRDT doesn't support state vectors to exchange a minimal amount of information. That only makes things easier and has little performance impact because awareness states are usually pretty small.