To couple presence to a specific document and ensure synchronization with document operations (preventing issues like cursor jitter), use DocPresence.
- Subscribe: Use
connection.getDocPresence(collection, id) to get a DocPresence instance and call .subscribe(). - Listen: Listen for the
'receive' event. An update of null indicates the remote client is no longer present. - Submit: Create a
LocalPresence via presence.create() and call .submit(value). The shape of the value is determined by the document type.
Note: Currently, only the rich-text type supports presence information.
const presence = connection.getDocPresence(collection, id)
presence.subscribe()
presence.on('receive', (presenceId, update) => {
if (update === null) {
// The remote client is no longer present in the document
} else {
// Handle the new value by updating UI, etc.
}
})
const localPresence = presence.create()
// The presence value depends on the type
localPresence.submit(value)