Let’s	
  build	
  offline-­‐first	
  apps!
James	
  Nocentini	
  |	
  Developer	
  Advocate,	
  Couchbase
©2014	
  Couchbase	
  Inc.
Use	
  case
2
• Clean	
  UI	
  
• The	
  user	
  shouldn’t	
  have	
  to	
  be	
  online
@jamiltz
No	
  loading,	
  no	
  waiting,	
  nothing.
©2014	
  Couchbase	
  Inc.
Options
4
@jamiltz
©2014	
  Couchbase	
  Inc.
Options
5
@jamiltz
6
Sync	
  Gateway Couchbase	
  Server
©2014	
  Couchbase	
  Inc.
Couchbase	
  Lite
7
Today	
  I’d	
  like	
  to	
  talk	
  about	
  some	
  of	
  the	
  internals	
  
of	
  Couchbase	
  Lite	
  Android
@jamiltz
©2014	
  Couchbase	
  Inc.
Saving	
  a	
  document
8
Map<String, Object> properties = new HashMap<String, Object>();

properties.put("type", "list");

properties.put("title", title);

properties.put("created_at", currentTimeString);

properties.put("owner", "profile:" + userId);

properties.put("members", new ArrayList<String>());
// save to database

Document document = database.createDocument();

document.putProperties(properties);
@jamiltz
©2014	
  Couchbase	
  Inc.
Saving	
  a	
  document
9
Document document = database.getDocument(myDocId);



String title = document.getProperty("title");



Map<String, Object> properties = doc.getProperties();

String owner = (String) properties.get("owner");
@jamiltz
©2014	
  Couchbase	
  Inc.
Saving	
  a	
  document
10
Document document = database.getDocument(myDocId);



String title = document.getProperty("title");



Map<String, Object> properties = doc.getProperties();

String owner = (String) properties.get("owner");
@jamiltz
Plain	
  	
  Old	
  	
  Java	
  	
  Objects
Plain	
  	
  Old	
  	
  JSON	
  	
  Objects
©2014	
  Couchbase	
  Inc.
It’s	
  JSON
{
"_id": "123",
"type": "list",
"title": "TodoMVC list",
"owner": "1234567890",
"members": [
"0987654321",
"0192837465"
]
}
13
@jamiltz
©2014	
  Couchbase	
  Inc.
Content	
  Types
JSON ❤ HTTP
14
@jamiltz
©2014	
  Couchbase	
  Inc.
What	
  else?
15
Replicators
@jamiltz
©2014	
  Couchbase	
  Inc.
Two	
  Verbs
Push	
  	
  →	
  
Pull	
  	
  	
  	
  ←
16
@jamiltz
©2014	
  Couchbase	
  Inc.
Two	
  Verbs
Push	
  	
  →	
  Server	
  
Pull	
  	
  	
  	
  ←	
  Server
17
@jamiltz
©2014	
  Couchbase	
  Inc.
Two	
  Verbs
Push	
  	
  →	
  Couchbase	
  Sync	
  Gateway	
  
Pull	
  	
  	
  	
  ←	
  Couchbase	
  Sync	
  Gateway
18
@jamiltz
©2014	
  Couchbase	
  Inc.
Push
19
facebookAuthenticator = AuthenticatorFactory.createFacebookAuthenticator(accessToken);



Replication push = database.createPushReplication(SYNC_URL);

push.setAuthenticator(facebookAuthenticator);

push.setContinuous(true);



push.start();
©2014	
  Couchbase	
  Inc.
If	
  you	
  were	
  to	
  build	
  your	
  own
▪ Create	
  a	
  job	
  object	
  that	
  has	
  an	
  action	
  
▪ Write	
  data	
  to	
  a	
  durable	
  queue	
  
▪ Run	
  a	
  service	
  to	
  process	
  the	
  queue	
  
▪ The	
  service	
  retries	
  if	
  the	
  network	
  hiccups	
  or	
  app	
  crashes	
  
▪ Remove	
  from	
  the	
  queue	
  once	
  the	
  job	
  is	
  complete
20
@jamiltz
©2014	
  Couchbase	
  Inc.
Replicators
21
Source Target
Android	
  App Sync	
  Gateway
@jamiltz
©2014	
  Couchbase	
  Inc.
Replicators
Get	
  the	
  sequence	
  ID	
  
of	
  the	
  source	
  database
22
@jamiltz
©2014	
  Couchbase	
  Inc.
Replicators
23
Source Target
Android	
  App Sync	
  Gateway
@jamiltz
seq:	
  15 seq:	
  5
©2014	
  Couchbase	
  Inc.
Pull
Pull	
  ==
24
Push
@jamiltz
©2014	
  Couchbase	
  Inc.
Replicators
25
Source
Android	
  App Sync	
  Gateway
@jamiltz
Target
©2014	
  Couchbase	
  Inc.
Replicators
26
Source
Android	
  App Sync	
  Gateway
@jamiltz
Target
seq:	
  15seq:	
  5
©2014	
  Couchbase	
  Inc.
Pull
27
facebookAuthenticator = AuthenticatorFactory.createFacebookAuthenticator(accessToken);



Replication pull = database.createPullReplication(SYNC_URL);

pull.setAuthenticator(facebookAuthenticator);

pull.setContinuous(true);



pull.start();
Demo
Don’t	
  drain	
  all	
  my	
  battery!
©2014	
  Couchbase	
  Inc.
Pull
30
facebookAuthenticator = AuthenticatorFactory.createFacebookAuthenticator(accessToken);



Replication pull = database.createPullReplication(SYNC_URL);

pull.setAuthenticator(facebookAuthenticator);

pull.setContinuous(true);



pull.start();
©2014	
  Couchbase	
  Inc.
Solution
GCM	
  is	
  your	
  friend	
   🎉
31
@jamiltz
©2014	
  Couchbase	
  Inc.
Data	
  Flow	
  Techniques	
  
32
Sync	
  Gateway
changes	
  feed
Push	
  notifications	
  
worker
Continuous	
  push
@jamiltz
©2014	
  Couchbase	
  Inc.
Data	
  Flow	
  Techniques	
  
33
Sync	
  Gateway
changes	
  feed
Push	
  notifications	
  
worker
Continuous	
  push
GCM	
  notification
GCM	
  notification
@jamiltz
©2014	
  Couchbase	
  Inc.
Data	
  Flow	
  Techniques	
  
34
Sync	
  Gateway
changes	
  feed
Push	
  notifications	
  
worker
Continuous	
  push
One	
  shot	
  pull
One	
  shot	
  pull
@jamiltz
Demo
©2014	
  Couchbase	
  Inc.
Conflict	
  resolution
36
rev-­‐1
rev-­‐2
rev-­‐1
rev-­‐2’
@jamiltz
©2014	
  Couchbase	
  Inc.
Conflict	
  resolution
37
rev-­‐1
rev-­‐2 rev-­‐2’
©2014	
  Couchbase	
  Inc.
Conflict	
  resolution
38
rev-­‐1
rev-­‐2 rev-­‐2’
current	
  revision
©2014	
  Couchbase	
  Inc.
Conflict	
  resolution
39
rev-­‐1
rev-­‐2 rev-­‐2’
©2014	
  Couchbase	
  Inc.
Conflict	
  resolution
40
rev-­‐1
rev-­‐2
current	
  revision
P2P Demo
©2014	
  Couchbase	
  Inc.
CouchbaseLite	
  Listener
42
Embedded	
  lightweight	
  HTTP	
  server
©2014	
  Couchbase	
  Inc.
Replications
43
Same	
  Replication	
  Interface	
  as	
  Sync	
  Gateway
©2014	
  Couchbase	
  Inc.
Mesh	
  Networks
44
Workshops
• Push	
  /	
  Pull	
  
• Basic	
  config	
  for	
  Sync	
  Gateway	
  
• And	
  more…
©2014	
  Couchbase	
  Inc.
Summary
▪ Performance	
  
▪ Ease	
  of	
  development	
  
▪ Live	
  Queries	
  
▪ Robustness
46
©2014	
  Couchbase	
  Inc.
More	
  Information
47
James	
  Nocentini	
  
Developer	
  Advocate	
  
@jamiltz	
  
ToDoLite	
  Android	
  
https://github.com/couchbaselabs/ToDoLite-­‐Android	
  
Couchbase	
  Developer	
  Forums	
  
http://forums.couchbase.com

Sync is hard: building offline-first Android apps from the ground up