yjs / y-protocols

Yjs encoding protocols
MIT License
109 stars 39 forks source link

setLocalStateField() stops working after reconnecting to the server #7

Closed hanspagel closed 3 years ago

hanspagel commented 3 years ago

Description When clients disconnect from e. g. a y-websocket server, the awareness states are removed. When clients reconnect to the server, and try to set the local state through setLocalStateField() that won’t happen, because there is no state to update.

As a result the list of connected users (= awareness states) stays empty after losing the connection to the server, even when it’s back online and all clients reconnected successfully.

Reproduction

  1. Open an editor demo with y-websocket and a list of connected users
  2. Stop the server
  3. Restart the server

Expected behavior I’d expect the clients to fetch up all awareness states again after a reconnect.

Environment Information

I’m sending a PR with proposed changes to fix this issue.

hanspagel commented 3 years ago

Workaround (in case someone is landing here):

provider.on('status', (event) => {
  if (event.status === 'connected') {
    // FIX: Reset the awareness state, before updating a single field
    provider.awareness.setLocalState({})
    provider.awareness.setLocalStateField('user', { name: 'Hans Pagel', color: 'pink' })
  }
})
dmonad commented 3 years ago

Hi @hanspagel

Thanks for opening this ticket and also providing a fix! It helped me to narrow down the problem.

The editor bindings shouldn't be able to overwrite your awareness state when you mark yourself as offline (i.e. awareness.setLocalState(null)). The problem was that in y-websocket, the client marked itself as offline when the connection closes. Instead, we will now keep the awareness state after a disconnect.

I fixed the problem in y-websocket@1.3.8

hanspagel commented 3 years ago

Oh, that was quick! And that’s definitely a better solution.

I updated to y-websocket@1.3.8 and can confirm it’s fixed. 🙌