yjs / y-webrtc

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

Error: A Yjs Doc connected to room "#room-name" already exists #47

Closed jacobcoro closed 1 year ago

jacobcoro commented 1 year ago

Describe the bug I'm getting an error when reconnecting and calling new WebrtcProvider() on a page refresh or after a disconnect.

To Reproduce Steps to reproduce the behavior:

  const provider = new WebrtcProvider(name, doc, {
    password,
    signaling: servers,
    awareness,
  });

Then call provider.disconnect() or provider.destroy(), or just try connecting again with the same code and params as above.

  1. Get error: Error: A Yjs Doc connected to room "#room-name" already exists

Expected behavior A new provider can be created and sync to the existing room without error. Should return the provider.

Environment Information

Screenshot 2023-05-06 at 18 16 48 Screenshot 2023-05-06 at 18 16 13 Screenshot 2023-05-06 at 18 15 51

Sorry not sure why sourcemaps aren't working, but I've found where it originates:

// src/y-webrtc.js
const openRoom = (doc, provider, name, key) => {
  // there must only be one room
  if (rooms.has(name)) {
    throw error.create(`A Yjs Doc connected to room "${name}" already exists!`)
  }
  const room = new Room(doc, provider, name, key)
  rooms.set(name, /** @type {Room} */ (room))
  return room
}
jacobcoro commented 1 year ago

Sorry. I realized I new WebRtcrRovider was getting called twice elsewhere in the code!