multiple room sync sub documents can not be acceptable on server side (if you have), because there can be too many connections. To support the main doc and many sub documents communicate in the same websocket connection, my idea is adding doc guid into sync message, something like below:
/**
* Listens to Yjs updates and sends them to remote peers (ws and broadcastchannel)
* @param {Uint8Array} update
* @param {any} origin
*/
this._updateHandler = (update, origin) => {
if (origin !== this) {
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, messageSync)
encoding.writeVarString(encoder, this.roomname) // identify which ydoc changed
syncProtocol.writeUpdate(encoder, update)
broadcastMessage(this, encoding.toUint8Array(encoder))
}
}
this.doc.on('update', this._updateHandler)
// sub document update handler
/**
* Listen to sub documents updates
* @param {String} id identifier of sub documents
* @returns
*/
this._getSubDocUpdateHandler = (id) => {
return (update, origin) => {
if (origin === this) return
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, messageSync)
encoding.writeVarString(encoder, id) // identify which sub document changed
syncProtocol.writeUpdate(encoder, update)
broadcastMessage(this, encoding.toUint8Array(encoder))
}
}
I have finished a poc, and it works.