Data Synchronization in AngularJS
Data Synchronization in AngularJS
Data Synchronization in
AngularJS
http://zndg.tl/ng-pouchdb
Daniel Zen
@zendigital
admin@zen.digital
My Background
Systems Architect, Developer & Trainer
Agile Engineering / XP Background
Started AngularJS-NYC Meetup mid-2012
Formed zen.digital in 2014
User Experience for Web & Mobile
Scalable MicroService based APIs
Best Practices for Development & Deployment
Corporate Training
Full Stack JavaScript
Node.js
MongoDb
etc.
Offline issues
local storage options
Sample Application walk through
http://github.com/danielzen/todo-ng-pouchdb
ng-PouchDb Collection & Binding
ABSOLUTELY
NOTHING
AngularJS is model agnostic
Any variable or object can serve as a
model
No unique ID required
No built-in persistence model for objects
ng-model binds javascript variables
to UI not to backend
SELEC GET
T
Update
(Modify)
UPDAT PUT /
E
PATCH
Delete
(Destroy)
DELET DELETE
E
PO
ST
crea
te
GET
read
PUT
upda
te
DEL
ETE
delet
e
/
acco
unts
Cre
ate
a
new
acc
ount
List
acco
unts
Bulk
upda
te
acco
unts
Dele
te all
acco
unts
/
acco
unts/
123
Erro
r
Sho
w
acco
unt
123
If
exist
s
upda
te
acco
unt
123
Dele
te
acco
unt
123
If not
error
$http Service
The AngularJS XmlHttpRequest API
follows the Promise interface ($q
service)
Returns a promise object with the standard
$http({method:
'GET',
'/someUrl'}).
then
method
andurl:
two
http specific methods:
success(function(data, status, headers, config) {
success
error.
// thisand
callback
will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error
status.
});
Conflict resolution
Intelligently merge documents with multiple
edits
Connectivity Lifecycle
Failures can happen on: client push, or client pull/
server push
Proprietary solution?
Firebase (AngularFire)
Firebase transparently reconnects to server
But doesnt persist to local storage
Firebase
Pros
AngularJS library (AngularFire)
3-way binding with $bind
Free Developer (Hacker) plan
Paid solution with premium support
Hosted solution
Can deploy static hosted apps
Cons
Proprietary solution
Hosted solution (cant run local or on own
servers)
No local storage solution
PouchDB
Pros:
Open Source
Lightweight Cross Browser JavaScript
implementation
Syncs with open source CouchDB protocol
servers
PouchDB-Server - a HTTP on top of PouchDB
Cloudant - A cluster aware fork of CouchDB
Couchbase Sync Gateway
Cons:
No Standard AngularJS library (yet)
angular-pouchdb is a decent wrapper
Data Model
(JavaScript)
Data Model
Data Model
(JavaScript)
Internet
Database
(Firebase)
Data Model
Data Model
(JavaScript)
Local Storage
(PouchDb)
Internet
Database
(CouchDb)
Pre-requisites:
Node
CouchDb
for 4-way sync
Resources
Data Persistence in Angular 2.0
Designing Offline - Alex Feyerke
Building offline applications with AngularJS and PouchDB
Sync multiple AngularJS apps without server via PouchDB
TodoMVC (AngularJS) + Hood.ie = 60 minutes to awesome