How can I know if all users have disconnected from y-websocket?

Is there any way to know when all of the users have disconnected from y-websocket?

On the server, I think you can import docs used internally by y-websocket and directly check if it is empty.

Docs are removed from the docs Map on disconnect:

If you are using the built-in server, you may need to copy server.js into your project and extend it to import docs.

Thanks for the reply, will try out this solution!

Is there any way I can do this on client side as well?
I’m using yjs.awareness to hold the data for users.
So I initially tried the .on(‘update’) but when the last user is disconnected it won’t be fired

Yes, you can use Awareness for that.

awareness.getStates() for getting the number of clients on-demand.

awareness.on(‘update’, …) for reactivity.

If it the last client is disconnected, then nobody is left to get an update event.

You can subscribe to WebsocketProvider status changes fwiw, and detect when a given client is disconnected. But necessarily a disconnected client cannot know which others clients are still connected.

Got it. Thanks for the help!

1 Like