The docs don’t go into the usage of awareness - I understand that this is because it’s unstable. But I’m trying to implement a feature where I show the active users editing a document.
This is what I’ve come up. I would love some feedback as to if this is the correct way to determine the active users, because it does seem to work.
Thank you for your help.
const awarenessProtocol = require('y-protocols/dist/awareness.cjs');
const awareness = new awarenessProtocol.Awareness(ydoc);
const provider = new WebrtcProvider(doc.id, ydoc, {
signaling: [
'wss://signaling.yjs.dev',
'wss://y-webrtc-signaling-eu.herokuapp.com',
'wss://y-webrtc-signaling-us.herokuapp.com',
],
password: null,
awareness,
maxConns: 20 + Math.floor(Math.random() * 15),
filterBcConns: true,
peerOpts: {},
});
provider.awareness.on('change', ({ added, updated, removed }, origin) => {
// logs an array of userIds
console.log(Array.from(provider.awareness.states).map((state) => state[1].userId));
});
provider.awareness.setLocalState({ userId });