How to do "multiple rooms" with Y-websocket?

I would like to know how one could do multiple rooms with Y-websocket. Right now, the code that I am running seems to get a single editor instance running at the time. That is, I have multiple docs on my website and they all go to the same editor. I was wondering how we open a new websocket/room instance for each document and also, how we can detect new users coming into that doc room instance. Is there a restriction on using other websocket packages such as socket.io?

Sorry, figured this out by reading the documentation a little more carefully. As shown in the following link: https://github.com/yjs/y-websocket
You can have a separate room simply by having a unique id for the room parameter that is in the wsProvider where the my-roomname in the parameter:

const wsProvider = new WebsocketProvider('ws://localhost:1234', 'my-roomname', doc)

Hey @notemaster,

As for your second question: The presence of a user can be detected using the Awareness protocol, which is documented here https://github.com/yjs/y-protocols

Here is an example on how to listen to the list of connected users. But you can also share data (like cursor position, email address, …) using the Awareness instance.

wsProvider.awareness.on('change', ({ added, removed, updated }) => {
  console.log('state updated:', updated)
  console.log('These users connected:' added)
  console.log('These users disconnected:' removed)

  console.log('All user states:' wsProvider.awareness.getStates())
})

// update local state
wsProvider.awareness.setLocalStateField('name', 'your name') // => "state updated"

About socket.io: The previous y-websocket connector used socket.io. But socket.io comes with a lot of dependencies, polyfills and things that you generally don’t need. I implemented a custom websocket wrapper that will work perfectly fine for most users. The only difference is that there will be no fallbacks. I.e. socket.io can fallback to http long polling if websocket is not supported. y-websocket won’t work in very old browsers, but websocket is now widely supported https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API#Browser_compatibility

1 Like

Does this work with room changes? I would assume on room changes it would trigger a removed and then an updated. Completely possible I’m doing something wrong :grimacing:

Contrary to the physical world, you can be present in several rooms at once :wink: You will be removed from the awareness state once you disconnect.

1 Like