yjs / y-protocols

Yjs encoding protocols
MIT License
109 stars 39 forks source link

Allow encoding awareness values as binary in updates #26

Open Myrannas opened 1 year ago

Myrannas commented 1 year ago

y-protocols/awareness currently encodes updates using a combination of writeVarString and JSON.stringify today.

The codebase I am working on uses a decent amount of single precision floating point numbers in awareness, and so would benefit from binary encoding. My understanding is that lib0 can encode arbitrary structures as binary using the writeAny method (used in yjs).

Backwards compatibility may be a little difficult with introducing binary encoding, as old clients would expect a JSON stringified value. To allow users of the library to control the rollout, it might be possible to introduce similar functionality to how YJS supports V1 and V2 updates - where an encoder is passed in as a parameter (with a default value).

For example:

class AwarenessEncoderV1 {
  encodeAwareness(state, encoder) {
    encoding.writeVarString(encoder, JSON.stringify(state));
  }
}

...

export const applyAwarenessUpdate = (awareness, update, origin, encoder = new AwarenessEncoderV1()) => { ... }

Would you be open to accepting a contribution with these changes?

dmonad commented 1 year ago

Hi @Myrannas,

I'm not sure if this is worth the trouble right now.

FYI: We are experimenting with several ideas to replace the current (extremely simple) Awareness CRDT. A v2 based on any-encoding would only slightly decrease the size of messages in most cases (Awareness is mainly used for syncing small updates like cursor positions and user names).

I'd accept a PR that adds the suggested optional parameter for a custom Awareness encoder. At this time, I'd not accept a PR that switches the default awareness protocol for a (slightly improved) v2 encoder. I don't think it's worth the trouble to switch the protocol right now. We are currently trying to maintain compatibility with the y-crdt project. I'd like to wait for a much-improved Awareness implementation before we change the default protocol.

Myrannas commented 1 year ago

Thanks for the heads up!

Makes sense for a lot of teams it may not make a large difference. For my use case, we anticipate this would make a large difference as we are using it to share some information like the current selection state (which can be 1000s of items).

I'll prepare a PR that would allow for a custom encoder, but leave the default as is.

Is there anywhere to read about the improved awareness implementation? I'd love to see what the plan is for that.