Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for obtaining the current online client #44

Merged
merged 2 commits into from
Mar 21, 2023

Conversation

fEyebrow
Copy link
Contributor

I had this situation: yjs/yjs-demos#16.

To avoid the initial content being duplicated, the client needs to know if it is the first to join. Only the first to join needs to set the initial content. So I added the function.

Here is my client code:

provider.on('synced', (val: { synced: boolean }) => {
  console.log('synced', val.synced)
  status.value = val.synced ? 'connected' : 'disconnected'
  isInitContent = true
})

let isInitContent = false
provider.signalingConns[0].on('message', (m: any) => {
  if (m?.data?.type === 'announce' && m?.number <= 1 && !isInitContent) {
    isInitContent = true
    const currentContent = editor.value?.getHTML()
    if (currentContent === '<p></p>') {
      editor.value?.commands.setContent(content, true)
    }
  }
})

@dmonad
Copy link
Member

dmonad commented Mar 21, 2023

Hi @fEyebrow,

This will work fine for local testing. But if the delay is more significant, this approach will fail when two clients connect simultaneously to the signaling server. Once "the first client" announces themselves, the "room" might already have two users listening to messages.

There needs to be more to solve the underlying problem. I still suggest you go with the solution described in yjs/yjs-demos#16.

However, happy to include the number of connected clients in the published messages. Please rename message.number to message.clients.

@fEyebrow
Copy link
Contributor Author

@dmonad
I renamed message.number to message.clients

Thanks for your answer.
If I go with the solution described in yjs/yjs-demos#16, I need to save the Yjs doc when the article is created, and then edit it later based on the saved Yjs doc, just like git clone the same source. After that, when a new user joins, the content will not be duplicated, is that right?

@dmonad
Copy link
Member

dmonad commented Mar 21, 2023

If I go with the solution described in yjs/yjs-demos#16, I need to save the Yjs doc when the article is created, and then edit it later based on the saved Yjs doc, just like git clone the same source. After that, when a new user joins, the content will not be duplicated, is that right?

Exactly 👍

@dmonad dmonad merged commit ac29b5d into yjs:master Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants