yjs / y-webrtc

WebRTC Connector for Yjs
MIT License
448 stars 109 forks source link

Duplicate 'close' listeners for WebRTC connection #21

Closed holtwick closed 3 years ago

holtwick commented 3 years ago

These lines seem to be wrong: https://github.com/yjs/y-webrtc/blob/master/src/y-webrtc.js#L222-L225

The result of it is, that peers are not removed correctly anymore.

dmonad commented 3 years ago

Right. I duplicated that because the close handler is not called when the error event is called. I should have combined that with the previous handler. Although I don't see how this can lead to issues?

holtwick commented 3 years ago

After I removed the part completely my issues were gone. The problem was that peers message was emitted, first to with payload telling to remove the peer and then afterwards another one that tried to add it again.

Maybe somewhere here? https://github.com/yjs/y-webrtc/blob/e0d5d11ec49de741b097f59283a0fb7fdf690899/src/y-webrtc.js#L267-L277

dmonad commented 3 years ago

Maybe this is a misunderstanding. The intention is that y-webrtc automatically handles (and reconnects) connections unless you explicitly disconnect the provider instance (then no connection should be possible anymore).

So when the connection to a peer is closed, then it will automatically subscribe to find more peers again.

holtwick commented 3 years ago

Whatever the reason was, your change fixed the issue. I had two browser windows being connected via y-webrtc and when reloading one of them the other one showed 2 connections. This is fixed now, probably due to the duplicate listening to 'close' being removed. Thanks!

dmonad commented 3 years ago

Oh nice! It makes sense as I simply assumed the second handler would be called after the first one (which might not be the case). Then I will publish a new release of this package as it seems to fix an issue.