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

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