[Starter Question] Clients sync status to server

Hi, all,

I am trying to make a very simple demo. The enviroment from my side is:

  1. Mac OS runs y-websocket’s sample server (server.js under bin folder) and also a client (called initiator).
  2. Windows VM runs another client (called collaborator).

The code for both clients is

let yjsDoc = new Y.Doc();
let yjsArray = yjsDoc.getArray("array");
if (role == "initiator") {
	let status = await connectToServer(yjsDoc, 'ipaddr', 1234, 'demo');
	await processInitiatorTasks(yjsArray);
} else {
	let status = await connectToServer(yjsDoc, 'ipaddr', 1234, 'demo');
	await processCollaboratorTasks(yjsArray);
}		

inside processInitiatorTasks function, initiator tries to add several integers to the array,

yjsArray.push([0]);
yjsArray.push([1]);

and in processCollaboratorTasks function, collaborator tries to get the numbers from the array

yjsArray.get(0);
yjsArray.get(1);

However, the collaborator was unable to read those update from the initiator.

My question is: after the initiator pushed new number to the array, how can it notify the server to make update on the server?

Thanks in advance.
Alex

Hi, Alex.

I think the three peers (two clients and one server) in the scenario you described need to share at least the same document, that is, have the same guid.

const doc = new Y.Doc({guid: ‘the id of doc’});
1 Like

Hi, @jarone, thanks for your quick reply. The problem is: when I define the array/text using ydoc, I haven’t connected to the server…

In this way, it will not read through the server anyway.

Thanks anyway for your help!
Alex

The client will always do a sync with the server when it connects. So elements that you have inserted before will be synced. Maybe you can start with some of the base templates that have a working setup before building your own connection logic.

A Collaborative Editor - Yjs Docs - contains a very basic example at the bottom.

Sure, will look at it.

Thanks,
Alex