Updates to the db and syncing clients
Building a collaborative platform that does rapid read/writes to a database that must sync across clients, can i use the update documents method in the docs?
8 Replies
Hey @Arta, yeah you can use Convex for this use case. Check out the quickstart or tutorial for how things works in general (you’ll want to write queries and mutations to encode your reads and writes to the db). Let me know if you have any questions.
Took a quick look at the docs and reads/writes etc. Our frontend is Angular so the react lib wouldn’t work, but the js one would
Is there a way to “subscribe” to the db for document changes?
And how would we go about merging JSON fields of a document with 2 concurrent mutations? Or is that handled already by Convex
Yes! Queries in convex are reactive: whenever any if the documents you access in a query are updated, the query result will update. You can subscribe to these changes with the JS client, the method is
client.onUpdate()
.
Can you say more about merging JSON fields and concurrency? You can merge document fields in a query function, and concurrency is automatic for mutations.Thanks! Basically our JSON documents look like the following:
Don't worry about the api key in the object, it's not private
People would be collaboratively working on these, I just wanted to know if concurrent writes are being done to this document, if it would propagate / merge the fields correctly out of the box
or if custom logic is needed
Convex provides serializable isolation guarantees for mutations via optimistic concurrency control, which means that if two functions read and write the same document at the exact same time, one will succeed, and the other will fail, be retried, and read the newer version when it tries again.
Does that make sense?
I personally would consider storing smaller objects with references. Eg having a table for characters, worlds, scenes, etc.. Then you can just store the IDs referencing other tables and have less likelihood of write conflicts when doing multiplayer editing
Makes sense! That is better
Thank you, I was just wondering the best way to go about handling large JSONs with convex
so this really clears it up