WebsocketProvider state `updated` with no change

Hello! I’m experimenting with a variation of the example code in the y-websocket README (see below[1]).

There are a couple of things I don’t understand:

  1. The updated callback occurs frequently (every 5 seconds or so) but has no real updates… the state is the same as it was the previous time.
    e.g.
state updated: Array [ 1891043866 ]
All user states: Map { 1891043866 → {}, 2718673154 → {} }

state updated: Array [ 2718673154 ]
All user states: Map { 1891043866 → {}, 2718673154 → {} }

state updated: Array [ 1891043866 ]
All user states: Map { 1891043866 → {}, 2718673154 → {} }

state updated: Array [ 2718673154 ]
All user states: Map { 1891043866 → {}, 2718673154 → {} }

state updated: Array [ 1891043866 ]
All user states: Map { 1891043866 → {}, 2718673154 → {} }
  1. The connection awareness seems very slow. It takes about 15 seconds to recognize that another localhost browser has connected?

My expectation is that a localhost WebSocket connection should be very fast (<100ms). Thanks!

[1] My variation of README example code:

import * as Y from 'yjs'
import { WebsocketProvider } from 'y-websocket'

const doc = new Y.Doc()

const wsProvider = 
  new WebsocketProvider('ws://localhost:1235', 'ayanarra', doc)

wsProvider.on('status', event => {
  console.log(event.status, 'I am', doc.clientID)
})

wsProvider.awareness.on('change', ({ added, removed, updated }) => {
  if  (added.length > 0) {
    console.log('These users connected:', added)
  }
  if (removed.length > 0) {
    console.log('These users disconnected:', removed)
  }
  if (updated.length > 0) {
    console.log('state updated:', updated)
    console.log('All user states:', wsProvider.awareness.getStates())
  }
}

The updated callback occurs frequently (every 5 seconds or so) but has no real updates… the state is the same as it was the previous time.

That’s right. The update function triggers even if no updates occur. Thanks for letting me know. I created a ticket for the issue Awareness update tiggered even without change. · Issue #2 · yjs/y-protocols · GitHub

  1. The connection awareness seems very slow. It takes about 15 seconds to recognize that another localhost browser has connected?

That doesn’t seem right. It should show the other users immediately. Can you please share details about your environment (browser, os, …)? Does the same issue happen to you on yjs.dev or demos.yjs.dev?.

Thanks for marking the updated callback issue down.

With regard to the connection awareness, I’m using Mac OS X and Firefox latest. I should try in Chrome. If the results are any different I’ll be sure to report it.

When I visit yjs.dev there is no delay (apart from loading the javascript/webpage).

One thing I’ve noticed is that even though the awareness callback is slow, the actual connection is happening quickly–i.e. I can see updates from each browser tab right away. So it isn’t that the browsers are having difficulty connecting to the websocket server, it appears that I’m just not getting notified immediately when they do.

I think I’ve identified the issue:

  • if a connecting client does not call wsProvider.awareness.setLocalStateField(...) then the presence of the client does not propagate immediately.
  • in that case, there seems to be some kind of timeout happening every 10 seconds or so that updates the awareness.state with just a very basic identifier.
  • because I just wanted a count of connected users, I didn’t think to setLocalStateField with anything, and I encountered the “slow” fallback behavior.