Device Synchronization with Javascript and PouchDB

Preview:

DESCRIPTION

Lightning talk describing how to give synchronization and offline capabilities to native mobile and desktop aplications. The main idea is to take advantage of PouchDB a database that syncs and that can be embedded in any Javascript application. This talk is based on real-life usages from the Cozy.io Personal Cloud.

Citation preview

Your devices deserve more than your files

v

Setup

$ npm install pouchdb ­­save

var pouchdb = require('pouchdb');  

<script src="pouchdb.min.js"></script>

Synchronization

var db = new PouchDB('todos');var remoteCouch = 'https://mycouch/todos';var opts = {live: true};

db.sync(remoteCouch, opts)  .on('change', onChange)  .on('uptodate', onUpdate)  .on('error', onError);

Conflicts

var opts = {conflicts: true};db.get('docid', opts, function (err, doc) {  var rev = doc._rev;  var conflictRev = doc._conflicts[0];  rev = selectRevision(rev, conflictRev);

  opts = {rev: rev};  db.get('docid', opts, function (err, doc) {     db.put(doc);  });};

Messaging (pub/sub)

function onChange (change) {

 if(change.doc.type === 'message'    && change.doc.chan === 'mychan') {           console.log(doc.content);

   db.put({    type: 'message',    content: 'Got it! Now I publish'   });  }

pouchdb/pouchdb

pouchdb/pouchdb-server

colinskow/pouch-mirror

natevw/PeerPouch

cozy-labs/[mobile|desktop]

pouchdb.com

Recommended