A few questions around awareness

Hi again!

I am trying to figure my way around the awareness protocols generally:

How to sync only local changes?

I want the responsibility of syncing the doc state to a remote server to be from the client that made the change. I want to do this to try to prevent some concurrent writes, as all the users will share one file. Hopefully at the end of the day the most recent copy will always get synced.

I have it kind of working:

  const handleOnChange = ({ added, updated, removed }, origin) => {
    if (origin === 'local') {
      console.log('I did this change');
      saveState();
    }
  };
  provider.awareness.on('change', handleOnChange);

I find though that things don’t always fire a change event. I cant seem to tell which kind of events cause it to fire. I’d love to be able to always know when the doc changes, and when it changes because of the local client, and then fire a debounced save method.

Edit:

I’ve found that this is generally working for syncing only local changes to the remote server:

 doc.on('update', (message, origin) => {
    if (origin?.key === 'y-sync$') {
      saveState();
    }
  });

How to get active participants from awareness?

I see that the added, updated, and removed arrays are sets of client IDs. I am not sure when the various events fire: change, update for the awareness protocol. Any help here would be so appreciated!

Hi @aulneau,

A sort overview: The Awareness instance handles presence information (e.g. who is currently online and where are their cursor information). It is completely independent from the Y.Doc. The Y.Doc maintains the different shared data types. And you are right, the Y.Doc ‘update’ event will fire every time a change happens.

Regarding the presence information (who is currently online), you can have a look at the Awareness documentation here: https://github.com/yjs/y-protocols/