# Collab with a nosql db and without server side code

**URL:** https://discuss.prosemirror.net/t/collab-with-a-nosql-db-and-without-server-side-code/566
**Category:** Uncategorized
**Created:** [January 10, 2017, 11:03pm UTC](https://discuss.prosemirror.net/t/collab-with-a-nosql-db-and-without-server-side-code/566 "2017-01-10T23:03:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![kofifus](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/kofifus/32/288_2.png) [@kofifus](https://discuss.prosemirror.net/u/kofifus)
#### Post date: [January 10, 2017, 11:03pm UTC](https://discuss.prosemirror.net/t/collab-with-a-nosql-db-and-without-server-side-code/566/1 "2017-01-10T23:03:45Z")

</div>

Has anyone looked into running collab with a nosql database (for example Firebase) without server side code ?

Is this feasible ? as far as I can tell it should be possible, as the authority is basically only doing set/get db transactions which can be fired directly from the client, but are there any other considerations ?

Any thoughts/tips/code appreciated …

---

<div class="post-metadata">

### Author: ![frederik](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/frederik/32/36_2.png) [@frederik](https://discuss.prosemirror.net/u/frederik)
#### Post date: [January 12, 2017, 7:40am UTC](https://discuss.prosemirror.net/t/collab-with-a-nosql-db-and-without-server-side-code/566/2 "2017-01-12T07:40:35Z")

</div>

I think it’s generally possible (though handling of error states etc would have to happen in the client & clients would have to trust each other).

Your clients would need to make sure that they access a synced global version. To not overwrite each other, it probably makes sense to add a rule in the firebase backend that makes sure that a client update is based on the same version the current firebase (kinda like the \_rev in couchdb). It’ll be a hack but probably workable.

Regards Frederik

p.s.: The client libraries for firebase are amazing - I’ve never written prototypes more easily since they do all the syncing of collections for me. Having said that, at some point I always needed a server to add business logic I couldn’t express in rules.

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [January 12, 2017, 1:47pm UTC](https://discuss.prosemirror.net/t/collab-with-a-nosql-db-and-without-server-side-code/566/3 "2017-01-12T13:47:40Z")

</div>

If your database allows some kind of compare-and-swap or if-not-matches header equivalent (to only increase a counter if the old value was the expected one), you can so this. But as Frederik noted, a lot of the intelligence will have to move to the client, which can not generally be trusted. And things like saving a whole snapshot of the document will have to be done out-of-band, alongside the step data, since the server won’t be able to compute a document from the steps.

---

<div class="post-metadata">

### Author: ![kofifus](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/kofifus/32/288_2.png) [@kofifus](https://discuss.prosemirror.net/u/kofifus)
#### Post date: [January 14, 2017, 6:05am UTC](https://discuss.prosemirror.net/t/collab-with-a-nosql-db-and-without-server-side-code/566/4 "2017-01-14T06:05:01Z")

</div>

Thanks Marijn !

I am trying to understand what you mean by “compare-and-swap or if-not-matches header equivalent (to only increase a counter if the old value was the expected one)” …

Is this for the line `if (version != this.steps.length) return` in `Authority.prototype.receiveSteps` ?

if yes, can receiveSteps be replaced with a client issued DB transaction that will first get the head revision and only write further steps to the DB if the head is equal to the revision of the last step that was pulled previously ? will this work ?

(I am ok with trusting the clients as this is the nature of serverless apps like Firepad, also I am ok with clients saving snapshots)

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [January 14, 2017, 11:00am UTC](https://discuss.prosemirror.net/t/collab-with-a-nosql-db-and-without-server-side-code/566/5 "2017-01-14T11:00:43Z")

</div>

> [@kofifus](#):
>
> if yes, can receiveSteps be replaced with a client issued DB transaction that will first get the head revision and only write further steps to the DB if the head is equal to the revision of the last step that was pulled previously ? will this work ?

Sounds like it would (though I don’t have much experience with this kind of db)

---

<div class="post-metadata">

### Author: ![kofifus](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/kofifus/32/288_2.png) [@kofifus](https://discuss.prosemirror.net/u/kofifus)
#### Post date: [January 29, 2017, 6:23am UTC](https://discuss.prosemirror.net/t/collab-with-a-nosql-db-and-without-server-side-code/566/6 "2017-01-29T06:23:40Z")

</div>

Here’s a working demo of collab with firebase and no server side:

[plunkr](https://plnkr.co/edit/X03q9bHbSfIswLqjY5Jf?p=info)

you can open it in two windows and see the collab in action.

Still work in progress so may be buggy but seems to be working fine. This is using an anonymous access db path in my personal firebase. If it get’s abused I’ll remove it.

This is really cool, we can collab without server side code. Prosemirror is fantastic 🙂

---

<div class="post-metadata">

### Author: ![kofifus](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/kofifus/32/288_2.png) [@kofifus](https://discuss.prosemirror.net/u/kofifus)
#### Post date: [February 12, 2017, 5:33am UTC](https://discuss.prosemirror.net/t/collab-with-a-nosql-db-and-without-server-side-code/566/7 "2017-02-12T05:33:04Z")

</div>

I’ve updated the code to minimize database space, especially in the case of only one user typing. Steps will be merged on the database and as long as no other client is reading them will be added to a single version.

Demo: [plunkr](http://plnkr.co/edit/8i88xZkn6b2DcCGroxN6?p=preview)

I started a github project for this [here](https://github.com/kofifus/FireProse) and will post updates there (next challenge is snapshots).
