Clean up rooms on websockets-server

To learn yjs, I want to build a collaborative scatterplot. A user can create a new session (I.e. a new room) and share the resulting link with others.

There could potentially be a lot of rooms created; so there might be a lot of providers instantiated to the point where the websockets server can’t keep up anymore.

I understand, that the client side provider objects are destroyed once all tabs are closed, that used the object, but the room and doc will still exist on the server. Can those be destroyed after some time of inactivity? Are there other patterns to keep server load in check?

By default, y-websocket does not persist Docs, so as soon as you kill the server the Docs will be deleted (and if no clients are connected, they will be permanently deleted).

If you are using a provider for persistence, they will typically have a deleteDocument or clearDocument function that allows you to delete a Doc (i.e. room). It’s up to you to manage when/how rooms get deleted from storage.

The YPERSISTENCE feature that is built in to y-websocket uses y-leveldb, so you can delete rooms by importing getPersistence and then calling persistence.clearDocument.

Hello @raine and thanks for your answer! :slight_smile:

Killing the server to clean up rooms and connections feels like a hack though! Is this the official and recommended way to get a hold of server-load?

So if you’re just interested in clearing out Docs from memory while the server is running, you can import docs and do:

const doc = docs.get(name)
doc.destroy()
docs.delete(name)

I’m not aware of an official recipe for what you are asking, though it seems valuable.

Thanks for the insight.

I don’t really have experience with how quickly server-load is growing when just keeping the server running for a while. It might also be valuable to keep the rooms alive for all eternity!

However, sooner or later the admin would have to power-cycle the server or have some cleanup-functionality to keep server-load at a reasonable level.

Makes sense :slight_smile: