Sync 3D world with YJS

I’m trying to learn how YJS works because I want to synchronize objects of my 3D world (I use babylonJS and websockets).

I think I have to use ymap to store all of my objects. But when a user modify a 3D object, will the whole object be sent to the server or only the properties that have changed?

There is a lot of documentation for text editing but I cannot find anything for complex objects so I don’t know if it’s possible.

Maybe using socket event for all modification of an object would be easier?

For any value that is modified in a ymap, the whole value is included in the update. So if { a: { b: 1, c: 2 }} is changed to { a: { b: 1, c: 2.1 }}, then the update will contain the whole object { a: { b: 1, c: 2.1 }}.

However, you can nest ymaps:

root = doc.getMap(‘root’)
root.set(‘a’, new Y.Map())
root.get(‘a’).set(‘b’, 1)
root.get(‘a’).set(‘c’, 2)

Then an update of a single property only includes that one property:

root.get(‘a’).set(‘c’, 2.1)

You might want to check out SyncedStore which I believe allows mutating deep values without all the .get/.set boilerplate.

Note: Be careful how often you are making updates, since even small changes will balloon the history over time. A common technique is to use Awareness for real-time, ephemeral shared data, and throttled Doc updates as “commits”.

2 Likes