Description
Checklist
[ x] Are you reporting a bug? Use github issues for bug reports and feature requests. For general questions, please use https://discuss.yjs.dev/
[ x ] Try to report your issue in the correct repository. Yjs consists of many modules. When in doubt, report it to https://github.com/yjs/yjs/issues/
Is your feature request related to a problem? Please describe.
Every time a user makes an update, an update is sent to the server. As a result, a large number of database writes occur, which can put significant strain on the server and database.
When central server is written in serverless environment, db writes becomes costly due to the high volume of write operations.
Describe the solution you'd like
I would like to add a throttling option for updates.
Additional context
I customized y-websocket in my project, and it works well. Below is the code I added to y-websocket:
const THROTTLE_TIME_MS = 5000
// y-websocket
export class WebsocketProvider extends Observable<string> {
constructor(doc, ...) {
// ...
this._updates = [];
this._batchBroadcastUpdates = () => {
const mergedUpdate = Y.mergeUpdates(this._updates)
// refresh queue when updates sent
this._updates = [];
const encoder = encoding.createEncoder();
encoding.writeVarUint(encoder, messageSync);
syncProtocol.writeUpdate(encoder, mergedUpdate);
broadcastMessage(this, encoding.toUint8Array(encoder), false);
};
this._throttledBroadcast = throttle(() => {
this._batchBroadcastUpdates();
}, THROTTLE_TIME_MS);
this._updateHandler = (update, origin) => {
if (origin !== this) {
// add update queue
this._updates.push(update);
this._throttledBroadcast();
}
};
this.doc.on('update', this._updateHandler);
}
disconnect() {
this._batchBroadcastUpdates();
// ...
}
}
- I'm a sponsor 💖
- This feature is critical for my project.