How to distinguish which user triggered this update

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


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