Turn the Server into the New Personal Device with Node.js, Cozy and CouchDB

Today, the practice to host his web services on one’s own hardware is common among tech people. They can use personal CLI, home-made scripts and web apps to store and manage their data in a trusted location. This way, they gain productivity and improve their privacy. Unfortunately managing one’s home server requires time and efforts and is not feasible for non technical people.

Intro
picture

Today, the practice to host his web services on one’s own hardware is common among tech people. They can use personal CLI, home-made scripts and web apps to store and manage their data in a trusted location. This way, they gain productivity and improve their privacy. Unfortunately managing one’s home server requires time and efforts and is not feasible for non technical people.

That’s what Cozy is solving. Cozy is a simple UI that runs on top of your linux distro. It makes hosting personal web apps on your hardware super-easy. Users can manage their own server like a smartphone. They can add or remove web apps with a single-click, run backup easily and add features by writing new applications (Cozy brings an open app store based on Git). Finally, the hosted web apps can collaborate together and share the same data store, a CouchDB instance.

Now you know a little bit more about Cozy, we are going to see how it takes advantage of CouchDB to offer new capabilities to your personal cloud.. You will discover, in the following, how Cozy handles data storage, application collaboration, backup and device synchronization with the help of CouchDB.

Data System, document types and permissions

CouchDB is a JSON document storage appliance. It doesn’t type any documents. In Cozy, to facilitate the data management, every documents are typed via a “docType” field.

{% highlight javascript %} { docType:”Event”, startDate:”20131201”, endDate:”20131201”, description:”Bob’s birthday”} {% endhighlight %}

The Data System, a simple REST API, acts as a layer between the applications and the services provided by Cozy. This way It can manage the document types (make requesting easier) and applications access rights (what app can access to what document type). It also handles permission access to services: like the database, file storage, indexation and email sending.

App collaboration through the Change Feed

CouchDB provides a change feed that notifies subscribers every time a change occurs in the Database. The Data System listens to the feed, then it publishes them to every applications that listen to a pub/sub socket feed of its own (based on Axon a ZMQ-like messaging protocol). Every changes are published to channels corresponding to the changed document type.

Every app can use a library that allows them to listen to any changes published by the data system and to propagate them directly to the browser via websocket. This way even the frontend can listen to data changed by any applications and accord the UI respectively. Through this mechanism, Cozy applications communicate in real-time.

Intro picture

Server side exemple

{% highlight javascript %}

var RealtimeAdapter = require(‘cozy-realtime-adapter’); var CozyDataSystem = require(‘jugglindb-cozy-adapter’); var Event = require(‘./server/models/event’);

// This line propagate all task changes to the application browser UI. var realtime = RealtimeAdapter compound, [‘task.*’] // bind with client

// Here we react to any event changes from the server. realtime.on(‘Event.*’, function (err, docId) { // handle change from the server if(err !== null) { Event.find(docId, function(err, event) { CozyDataSystem.sendMail({ to:myfriend@contacts.com subject:’an event changed’ body:’start at ’ + event.startDate }); }; }; }); {% endhighlight %}

Client Side exemple

{% highlight html %} // HTML file {% endhighlight %}

{% highlight javascript %} // JS file var SocketListener = CozySocketListener.Model.extend({

models: { ‘event’: Event },

events: [‘event.create’, ‘event.delete’],

// When a new event is created remotely, we add it to the Backbone // collection. onRemoteCreate: function(model) { return this.collection.add(model); },

// When a new event is deleted remotely, we add it to the Backbone // collection. onRemoteDelete: function(model) { return this.collection.remove(model); }, });

var sl = new SocketListener(); sl.watch(myBackboneEventCollection); {% endhighlight %}

Backup

CouchDB provides instance-to-instance replication capabilities. It makes it super easy to backup your data by simply replicating the whole database to another Couch installation. For that you only need to provide the URL of your target instance. Cozy allows to use this feature via a simple shorcut through a CLI (see example below). As you understand, once your target instance is set up, backuping your personal cloud requires only a single action.

$ cozy-monitor backup https://login:password@backup.mydomain.me:5984

NB: This shortcut will be available soon through the Web UI.

Offline mode: data Proxy and File System (work in progress)

Cloud apps are great but they are not always available (when you work in the plane, in the train…). So we provide faciltitors to build data proxy on your laptop and on your mobile. Once you setup another CouchDB instance on your desktop computer or your your laptop, you can synchronize your Cozy data locally and work directly with it.

TouchDB for Android and iOS allows to do the same work on your smartphone. Once the data are synchronized with your cozy, your native applications can connect directly to the TouchDB instance and start the synchronization when the connection is back.

In that cases, both Couch and Touch can be considered as data proxy: they buffered your modifications and sync them when it can.

By the way it allows to do fun thing. Ex: once your Data proxy is deployed on your laptop, you can use our FUSE implementation that allows you to modify the files stored in Couch from your file explorer. Thanks to our real-time API, when you will add these files to a folder in your local file-system, it will appear in the browser UI quickly after.

Intro picture

Final thoughts

That’s all! We hope you enjoyed reading that article . We expect that it will give you good ideas of what to do with Couch and Cozy! Check our architecture page and try our application creation tutorial to see how you can use the full potential of your personal data.

To conclude, because CouchDB is a key component in the Cozy architecture we would like to take the opportunity of this blog post to say thank you to the CouchDB community for bringing us a such amazing software!