Is there a way to have two awareness objects?

So that I don’t make the mistake of asking the wrong question, let me preface with my intent:

In Relm (a 3D spatial platform) I have some “large-ish” data associated with each user, and I am currently using Awareness to send this to all users. We are sending some user data very infrequently (e.g. hair color, body shape, what object is equipped). Other data we send very frequently, such as the x,y,z coordinates of the avatar. However, I recently discovered that the y-websocket Awareness system re-transmits all local state on any local state change. This means that along with the “fast” data, we are re-transmitting the “slow” data at a frequency of about every 0.016 seconds. Not good.

One idea is to separate things into TWO awareness objects per user: one for “slow” updates (hair color, etc.) and one for “fast” updates (x,y,z coordinates every 0.016 seconds).

First, is this possible? And without creating another Y.Doc that would unnecessarily get updated?

Second, is this a good idea? Is there a better way that is within easy reach?

CEO/developer at Relm.US

Hi @canadaduane,

That’s certainly an option. The Awareness protocol is very simple, but not well suited for large datasets. Just an update: John Waidhofer wrote his bachelor thesis about a more performant Awareness CRDT that we want to use to handle incremental updates better. Currently, there’s only a Rust implementation and we haven’t added support for it yet in the providers.

In the meantime, it makes sense to separate the awareness state. You could simply use two awareness instances. The Awareness CRDT is completely isolated from the Yjs document. We only supply the Y.Doc to the Awareness CRDT because we want to reuse the clientID. That is probably something we can change.

I recommend creating two Awareness CRDTs that you sync over different “rooms”. Just supply the same Y.Doc to the Awareness CRDT, but sync it over a different “room”. E.g. like this:

const ydoc =  new Y.Doc() // let's assume this is the document that holds all your current state.
// you are probably still using the websocket Provider:
const provider1 = new WebsocketProvider(url, room, ydoc)
// you can create another awareness instance like this (reusing the client id from the original ydoc)
const otherAwareness = new Awareness(ydoc)
// you add a new provider that only syncs another awareness instance. The ydoc will never be updated.
const otherProvider = new WebsocketProvider(url, room + '/otherAwareness', new Y.Doc(), { awareness: otherAwareness })
1 Like