yjs / y-websocket

Websocket Connector for Yjs
https://docs.yjs.dev/ecosystem/connection-provider/y-websocket
MIT License
508 stars 258 forks source link

Can not close awareness listener when call provider.destory() #150

Open orangejava opened 1 year ago

orangejava commented 1 year ago

Please save me some time and use the following template. In 90% of all issues I can't reproduce the problem because I don't know what exactly you are doing, in which environment, or which y-* version is responsible. Just use the following template even if you think the problem is obvious.

Checklist

Describe the bug I use a rich text editor with collaborative editing named Textbus, it use yjs and y-websocket to complete collaborative function. I use this editor with y-websocket and deploy a y-websocket node server. Open the editor it will new WebsocketProvider. When more than two users edit together( A and B), it collaborate normally; when A user click v button, A user will disconnect the y-websocket and the editor will destroy, I call the provider.destroy() to disconnect the y-websocket at the same time, A user and B user disconnect the collaborative, but in the Browser console, I see the A and B still print the awareness info, the correct should B print A not. The provider.awareness is not destroy completely, it cause the two provider.awareness send the message to other user client when A user connect to the editor again. I call the provider.awareness.destroy() to close the awareness update, it should add this statement (provider.awareness.destroy()) in the provider.destroy() function or modify the awareness.off() function.

To Reproduce Steps to reproduce the behavior:

  1. open the two different account with the same editor
  2. Click a button and call provider.destroy() to disconnect the y-websocket connection.
  3. Open console, you will see the provider.awareness is still send message.
  4. When this editor is reconnect the y-websocket, it will exist two provider.awareness send message.

Expected behavior When user call the provider.destroy() the provider should be destroyed completely includes awareness.

Screenshots I'm sorry for this, due to the network limitation, I cannot provide the screenshots temporarily.

Environment Information

Additional context Add any other context about the problem here.

Huly®: YJS-569

a3626a commented 6 months ago

I think we are facing same issue.

Awarness is not removed when destroy is called. Awareness is removed only when the tab is closed

But I'm not sure this is a feature or a bug.

dmonad commented 5 months ago

When the provider is destroyed, it doesn't destroy the awareness instance. This is the intended behavior because other parts of your software may rely on the awareness instance.

If the awareness changes (e.g. moving of a cursor), is still propagated over y-websocket after calling provider.destroy(), this would be a bug. But I don't think this is the case here.

If you do awarenes.on('update', () => { console.log('update') }), you will receive updates forever, unless you destroy the awareness instance. It's not bound to the provider instance.

Special case: the awareness instance is destroyed when the associated ydoc is destroyed.

You can also call awareness.destroy() manually, if that is something you want to do.

However, I don't want to destroy the awareness instance when provider.destroy is called, because other providers may still use the awareness instance. Awareness instances can be shared between providers.

Just make sure that you don't use old awareness instances. Alternatively, you can supply awareness as an option to the WebsocketProvider, so the instance doesn't change when you recreate the provider. See https://github.com/yjs/y-websocket/?tab=readme-ov-file#api

In any case, I recommend that you do provider.disconnect instead of provider.destroy and recreating a provider.