stefanw / channels-yroom

Django Channels WebSocket consumer and worker for synchronizing Yjs clients
https://channels-yroom.readthedocs.io/en/latest/
MIT License
15 stars 5 forks source link

Autosave Feature #14

Closed atheler closed 10 months ago

atheler commented 1 year ago

Implementing autosave functionality which snapshots a room after a period of editing inactivity.

Motivation

A collaborative document gets stored in a database with additional versioning. It would be nice if the stored snapshots correspond to a stable state of the document. That is, in between edits.

Changes

A new AUTOSAVE_DELAY setting was added which controls the debounce delay per room. This is turned off by default.

YROOM_SETTINGS = {'my-document': {'AUTOSAVE_DELAY': 10.0}}

An additional Autosave object inside the YRoomChannelConsumer handles the debouncing and triggers the snapshots. This object spawns a waiter task for each room similar to the cleanup tasks of YRoomChannelConsumer.

Issue / Open Question

In order for this to work proper editing messages need to be distinguished from awareness / background messages. Unfortunately I am not familiar enough with the lib0 encoding scheme. For now a simple size threshold is in place inside Autosave.nudge_if_changed(). This needs refinement and I would be grateful for inputs pointing me in the right direction.

Closing

Let me know what you think and thanks again to @stefanw for the great work!

atheler commented 12 months ago

I updated the code for the use of the new YRoomMessage.has_edits flag from Adding has_edits Flag to YRoomMessage. The changes seem to work nicely together inside my current Django application and the unnecessary autosaves disappeared. The room forgetting has also been moved inside the cleanup task's remove_room method see discussion.

atheler commented 11 months ago

Why was the forward reference to YRoomChannelConsumer removed?

stefanw commented 10 months ago

Why was the forward reference to YRoomChannelConsumer removed?

Sorry, bad rebase when I tried to fix the style of the forward ref (black didn't like it).