Y.encodeStateAsUpdate based on origin?

Apologies if this is an obviously question – but it’s unclear to me from reviewing the docs.

Is there a way to ensure that the client only sends their own changes to the server?

Y.encodeStateAsUpdate seems to me to encode the entire local state (as compared to some target vector), including changes applied made by other clients. In certain instances, this is not preferred, b/c we trust the changes that each individual client makes, but we don’t want to therefore trust the changes that have been sent from one client to another – each client should send their own updates to the server, even if they have applied changes directly received from other clients to their local state.

tldr: is it possible to filter by transactionOrigin when encoding changes to send the the server?

If you are sending incremental updates to your server you can store and send your updates with something like this:

    doc.on("update", (update, origin) => {
      //check if the origin is local
      if (origin.key == "y-sync$") {
        updates.push(update)
      }
    });

This is saving the updates to an array and then you can use Y.mergeUpdates(updates) to merge the updates array before you send.

1 Like