Syncing clients / server via serverless functions

Hi, I’ve always asked myself if it was possible to synchronize clients via “serverless” functions / lambdas + a persistent database (I’m thinking hosted Postgres or even Firebase).

This example in the manual - https://github.com/yjs/yjs/blob/master/README.md#example-sync-two-clients-by-computing-the-differences - wouldn’t work, since the server has to keep track of what is the last state of each client.

I was thinking: wouldn’t it work for each client to:

1 - persist each doc update* via a call to a store lambda, which saves the update in the database
2 - periodically poll a get-updates lambda, which queries the db for updates. Each client knows the id of the last update received, so the query will just get the updates stored later**

My reasoning is that, since the updates can be applied in every order, the main concern of a client would be getting them all, but not necessarily in a particular order relatively to the document updates. Essentially, the two queries could work independently inside each client. The lambdas would just have to do dumb, sequential updates (store) and ranged queries to a database (get-updates).

Could it work? Or am I missing something fundamental?

(*) or every n updates, debouncing them
(**) A cron function could also run periodically on the lambda side, compacting the updates up to a certain id, but that’s an optimization.

1 Like

I’d consider to use webrtc for realtime sync and serverless functions for persistency only. Did you see ProseMirror/Yjs (y-webrtc) in a serverless architecture PoC ?

If I understand it correctly, the webrtc connector will sync clients only when they’re online, which is cool for collaboration but not if your target is synchronising different devices (which might be running one at a time), which is what I’m trying to do.

If a new client comes online it get the initial data from:

  • webrtc in case another client is already online
  • serverless function/database if no other client is online

Once clients are online, they share data via webrtc. Additionally, they write snapshots via serverless function, so that clients can come up again after all clients went offline.

1 Like