Device Synchronization with Javascript and PouchDB

16
Your devices deserve more than your files

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.

Transcript of Device Synchronization with Javascript and PouchDB

Page 1: Device Synchronization with Javascript and PouchDB

Your devices deserve more than your files

Page 2: Device Synchronization with Javascript and PouchDB
Page 3: Device Synchronization with Javascript and PouchDB

v

Page 4: Device Synchronization with Javascript and PouchDB
Page 5: Device Synchronization with Javascript and PouchDB
Page 6: Device Synchronization with Javascript and PouchDB
Page 7: Device Synchronization with Javascript and PouchDB
Page 8: Device Synchronization with Javascript and PouchDB
Page 9: Device Synchronization with Javascript and PouchDB
Page 10: Device Synchronization with Javascript and PouchDB
Page 11: Device Synchronization with Javascript and PouchDB

Setup

$ npm install pouchdb ­­save

var pouchdb = require('pouchdb');  

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

Page 12: Device Synchronization with Javascript and PouchDB

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);

Page 13: Device Synchronization with Javascript and PouchDB

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);  });};

Page 14: Device Synchronization with Javascript and PouchDB

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'   });  }

Page 15: Device Synchronization with Javascript and PouchDB

pouchdb/pouchdb

pouchdb/pouchdb-server

colinskow/pouch-mirror

natevw/PeerPouch

cozy-labs/[mobile|desktop]

pouchdb.com

Page 16: Device Synchronization with Javascript and PouchDB