Confused about awareness on('change') event

hey all ! i am a bit confused about the change event listener in y-protocols/awareness . it listen both remote and local change on awareness… i checked the sourceCode of y-websocket here: https://github.com/yjs/y-websocket/blob/master/src/y-websocket.js, the awareness code is as below:

this._awarenessUpdateHandler = ({ added, updated, removed }, origin) => {  // why not tell origin here
     const changedClients = added.concat(updated).concat(removed)
     const encoder = encoding.createEncoder()
     encoding.writeVarUint(encoder, messageAwareness)
     encoding.writeVarUint8Array(encoder, awarenessProtocol.encodeAwarenessUpdate(awareness, changedClients))
     broadcastMessage(this, encoding.toUint8Array(encoder))   // if comes from remote, why broadcast out, will lead to endless loop or not ...
}
awareness.on('change', this._awarenessUpdateHandler)  // both remote and local change

if received change from remote origin, it broadcast the change message back to server, will that lead to endless loop ?

Hi @zlv-thisF, good question.

You are right, all clients/servers broadcast all awareness updates.

The awareness protocol is a CRDT implementation itself. Every client can update its own awareness state by broadcasting a JSON-like object and attach an increasing clock to it. It looks somehow like this:

update1 = { clock: 0, state: {..} }
update2 = { clock: 1, state: {..} }
..

Remote clients will apply the awareness update only if the clock increased. Eventually, the propagation of an awareness update stops when all clients received the latest state.