How to distinguish which user triggered this update

:grinning:
Requirement: I need to distinguish which user triggered this event based on the information on the event in the observe callback function.
Scenario: I need to support speaker operation and follow operation for my collaboration function
Can anyone provide some suggestions or methods? I would be very grateful.

 YMap.observeDeep((eventArray, transaction) => {
      if (!transaction.origin) return;
    });

Wrap the YJS op in a transaction and set whatever properties you need for origin there. ex:

ydocRoot.transact( () => {
  do whatever ..
}, { origin props here }
)

Youā€™ll see the origin props in the observeDeep code. HTH.

1 Like

Is the origin meant to hold arbitrary metadata (e.g. a complex a JSON object)? Does it get persisted and associated with items in those transactions?

Yes and I assume yes. See the docs: Y.Doc | Yjs Docs

"doc.transact(function(Transaction): void [, origin:any])

Every change on the shared document happens in a transaction. Observer calls and the update event are called after each transaction. You should bundle changes into a single transaction to reduce event calls. I.e. doc.transact(() => { yarray.insert(ā€¦); ymap.set(ā€¦) }) triggers a single change event.
You can specify an optional origin parameter that is stored on transaction.origin and on(ā€˜updateā€™, (update, origin) => ā€¦)"

And in the YJS code:
yjs/src/utils/Doc.js at main Ā· yjs/yjs Ā· GitHub Line 166
yjs/src/utils/Transaction.js at main Ā· yjs/yjs Ā· GitHub Line 53

It is missing from: But is missing in docs for transactions: Shared Types | Yjs Docs

HTH

1 Like

Origins are not persisted, and they donā€™t tell you anything about the user who created the operation. The origin is useful to distinguish which component of your application generated the changeā€”mainly to avoid infinite loops. However, the undo manager also uses them.

The transaction only works locally. For example, if the y-websocket provider receives a change it will apply the change to the Y.Doc. The origin will be the the y-websocket provider object.

You can setup PermanentUserData to figure out which user created which content. There is no API to figure out which ā€œuserā€ created a specific change, because Yjs has no concept of users.

You can, however, distinguish local from remote changes using transaction.local.

1 Like