Couchbase Mobile Ideathon 
Traun Leyden 
Lead Android Developer 
tleyden@couchbase.com 
10/29/13
About me 
• I was previously at a startup company doing a 
mobile CRM app — we converted our app to 
Couchbase Lite to solve networking performance 
issues 
• Now I work full time as the lead developer on 
Couchbase Lite Android 
• I also work on demo apps on both iOS and Android 
• I have blog at http://tleyden.github.io
About Couchbase 
• Was the merger of two companies 
• CouchOne (commercial support for CouchDB) 
• Membase (a commercially supported / distributed 
version of memcached) 
• Our flagship product is Couchbase Server, a fault 
tolerant distributed database. 
• We recently released Couchbase Mobile, targeted 
towards mobile and embedded devices/sensors. 
• All of our products are released as Open Source 
software under the Apache2 license. 
• We now have an office in Japan
Why would you want to use 
Couchbase Mobile? 
Will your app work when the network is 
slow or only sometimes connected? 
?
We think our network is fast, but .. 
• On trains or underground 
• Large crowds 
• Out of the city center 
• Deployed on sites which have unreliable 
Wifi 
• Sometimes you are completely offline 
• Etc ..
The simplest use case — sending a 
tweet on the subway 
Users expect better than this!
Our solution - Sync
Your app reads/writes to a local 
database 
This will be fast even if the network is slow or offline
Couchbase Lite sync’s the data to the 
cloud (can run in background)
TodoLite Demo 
• TodoLite is a todo list app 
• Users can login via Facebook 
• Users can share todo lists with each 
other 
• Different users can both view and 
update the same list 
• Photo attachments can be added to 
Todo items 
• Supports conflict resolution 
• Source code available on github (see 
resources slide) 
• Available in iTunes app store and 
Google Play
Understanding sync gateway 
• Sync Gateway can control which 
users can see which documents. 
• In effect, it can partition the 
database so that users only see a 
subset of the database
Partitioning a database by company 
and region
Using Sync Gateway to partition a 
database 
• Sync Gateway can do this efficiently 
• The main reason it was created 
• Sync Gateway uses “channels” to do 
this 
• Let’s walk through an example
Using Channels in 
OfficeRadar Example 
OfficeRadar uses iBeacons to detect who is in the office
Users should only see activity for other 
users in their organization
Sync Function 
• The sync function partitions the database by 
putting documents into channels 
• To understand the sync function, we must 
look at the OfficeRadar documents
OfficeRadar GeofenceEvent Document 
Each Geofence Event is associated with an 
organization 
{ 
"_id":"!QJoBU7cRa~m2E2tYloSHHN", 
"_rev":"1-8e8f3cebf357a2546c0d40e497227115", 
"action":"entry", 
"beacon":"4a83813db6ce76e9618793cf483cfa10", 
"created_at":"2014-08-28T23:43:19.119Z", 
"userprofile":"10152586863333982", 
"type":"geofence_event", 
"organization":"couchbase" 
}
OfficeRadar UserProfile Document 
Each User Profile is associated with an 
organization 
{ 
"_id":"10152586863333982", 
"_rev":"1-2b6a6e182ef77cd15db7171023931e8d", 
"type":"profile", 
"authSystem":"facebook", 
"name":"Traun Leyden", 
"organization":"couchbase" 
}
OfficeRadar Sync Function 
function(doc, oldDoc) { 
if (doc.type == "geofence_event") { 
channel(doc.organization); 
} else if (doc.type == "profile") { 
access(doc.userid, doc.organization); 
} 
}
Adding a geofence event to a channel 
if (doc.type == "geofence_event") { 
channel(doc.organization); 
}
Allowing a user to access an 
organization’s channel 
else if (doc.type == "profile") { 
access(doc.userid, doc.organization); 
}
Views 
Putting the “No” in NoSQL 
• Map/Reduce mechanism 
- A standard method of indexing in NoSQL 
- A view is similar to index in relational database. 
• App-defined map function 
- Called on every document 
- Can emit arbitrary key/value pairs into the index 
• Optional reduce function 
- Data aggregation / grouping 
• Functions are registered as native callbacks 
- Native callbacks make sense for performance and to match the rest of 
the app codebase
Queries 
• Basic feature set 
- Key ranges, offset/limit, reverse, group by key… 
- No joins or fancy sorting 
- but compound keys (and clever emits) allow for some tricks 
• LiveQuery subclass 
- Monitors a view for changes 
- Can think of it as a “pub-sub” approach 
- Register a callback that is triggered when the query changes
Use Cases
Embedded 
Picsolve 
 Image capture of roller 
coaster riders to local 
database in multiple 
theme park locations 
 Machine to machine 
communication example 
 Unreliable network 
connectivity required 
offline capture / later sync 
to centralized 
management system
Infinite Campus 
Education / Social Interaction 
 Selectively push out learning 
modules / multimedia lessons 
to individual students 
 Teachers and students use 
custom mobile chat apps for 
real-time Q&A during lectures 
 Homework assignments can be 
completed offline anywhere
Offline Sensor Data 
A weather balloon could collect sensor data while 
offline and sync it whenever it is online
Travel Expense Tracker 
• I am in Tokyo, but my iPhone 5 is locked so I cannot use a 
local SIM card. 
• But I need an app to track my expenses, so I must be able to 
enter expenses while offline. 
• When I get back to the US, I want to sync these expenses to 
the Cloud so I can download a spreadsheet.
Resources 
• These slides will be made available on SlideShare 
• Sample code links 
• https://github.com/couchbaselabs/ToDoLite-Android 
• https://github.com/couchbaselabs/ToDoLite-iOS 
• https://github.com/tleyden/office-radar 
• Documentation 
• http://developer.couchbase.com/mobile 
• Download 
- http://mobile.couchbase.com 
• Contact: couchbasejp@couchbase.com
Resources 2 
• Couchbase Mobile Google Group (community): 
groups.google.com/group/mobile-couchbase/ 
• Couchbase Mobile Twitter: 
https://twitter.com/CouchbaseMobile 
• Run Sync Gateway (and Couchbase Server!) under Docker: 
http://tleyden.github.io/blog/2014/06/22/running-couchbase- 
sync-gateway-on-gce/
Upcoming Event: Hackathon 
• http://couchbasejpcommunity.doorkeeper.jp/events/13988
Upcoming Event: 
Couchbase Server 
• Couchbase Server 
http://couchbasejpcommunity.doorkeeper.jp/events/14785
Thanks for watching! 
Contact: tleyden@couchbase.com 
Questions?

Couchbase Mobile Ideathon in Tokyo 2014.08.29: Developing with couchbase lite

  • 1.
    Couchbase Mobile Ideathon Traun Leyden Lead Android Developer tleyden@couchbase.com 10/29/13
  • 2.
    About me •I was previously at a startup company doing a mobile CRM app — we converted our app to Couchbase Lite to solve networking performance issues • Now I work full time as the lead developer on Couchbase Lite Android • I also work on demo apps on both iOS and Android • I have blog at http://tleyden.github.io
  • 3.
    About Couchbase •Was the merger of two companies • CouchOne (commercial support for CouchDB) • Membase (a commercially supported / distributed version of memcached) • Our flagship product is Couchbase Server, a fault tolerant distributed database. • We recently released Couchbase Mobile, targeted towards mobile and embedded devices/sensors. • All of our products are released as Open Source software under the Apache2 license. • We now have an office in Japan
  • 4.
    Why would youwant to use Couchbase Mobile? Will your app work when the network is slow or only sometimes connected? ?
  • 5.
    We think ournetwork is fast, but .. • On trains or underground • Large crowds • Out of the city center • Deployed on sites which have unreliable Wifi • Sometimes you are completely offline • Etc ..
  • 6.
    The simplest usecase — sending a tweet on the subway Users expect better than this!
  • 7.
  • 8.
    Your app reads/writesto a local database This will be fast even if the network is slow or offline
  • 9.
    Couchbase Lite sync’sthe data to the cloud (can run in background)
  • 10.
    TodoLite Demo •TodoLite is a todo list app • Users can login via Facebook • Users can share todo lists with each other • Different users can both view and update the same list • Photo attachments can be added to Todo items • Supports conflict resolution • Source code available on github (see resources slide) • Available in iTunes app store and Google Play
  • 11.
    Understanding sync gateway • Sync Gateway can control which users can see which documents. • In effect, it can partition the database so that users only see a subset of the database
  • 12.
    Partitioning a databaseby company and region
  • 13.
    Using Sync Gatewayto partition a database • Sync Gateway can do this efficiently • The main reason it was created • Sync Gateway uses “channels” to do this • Let’s walk through an example
  • 14.
    Using Channels in OfficeRadar Example OfficeRadar uses iBeacons to detect who is in the office
  • 15.
    Users should onlysee activity for other users in their organization
  • 16.
    Sync Function •The sync function partitions the database by putting documents into channels • To understand the sync function, we must look at the OfficeRadar documents
  • 17.
    OfficeRadar GeofenceEvent Document Each Geofence Event is associated with an organization { "_id":"!QJoBU7cRa~m2E2tYloSHHN", "_rev":"1-8e8f3cebf357a2546c0d40e497227115", "action":"entry", "beacon":"4a83813db6ce76e9618793cf483cfa10", "created_at":"2014-08-28T23:43:19.119Z", "userprofile":"10152586863333982", "type":"geofence_event", "organization":"couchbase" }
  • 18.
    OfficeRadar UserProfile Document Each User Profile is associated with an organization { "_id":"10152586863333982", "_rev":"1-2b6a6e182ef77cd15db7171023931e8d", "type":"profile", "authSystem":"facebook", "name":"Traun Leyden", "organization":"couchbase" }
  • 19.
    OfficeRadar Sync Function function(doc, oldDoc) { if (doc.type == "geofence_event") { channel(doc.organization); } else if (doc.type == "profile") { access(doc.userid, doc.organization); } }
  • 20.
    Adding a geofenceevent to a channel if (doc.type == "geofence_event") { channel(doc.organization); }
  • 21.
    Allowing a userto access an organization’s channel else if (doc.type == "profile") { access(doc.userid, doc.organization); }
  • 22.
    Views Putting the“No” in NoSQL • Map/Reduce mechanism - A standard method of indexing in NoSQL - A view is similar to index in relational database. • App-defined map function - Called on every document - Can emit arbitrary key/value pairs into the index • Optional reduce function - Data aggregation / grouping • Functions are registered as native callbacks - Native callbacks make sense for performance and to match the rest of the app codebase
  • 23.
    Queries • Basicfeature set - Key ranges, offset/limit, reverse, group by key… - No joins or fancy sorting - but compound keys (and clever emits) allow for some tricks • LiveQuery subclass - Monitors a view for changes - Can think of it as a “pub-sub” approach - Register a callback that is triggered when the query changes
  • 24.
  • 25.
    Embedded Picsolve Image capture of roller coaster riders to local database in multiple theme park locations  Machine to machine communication example  Unreliable network connectivity required offline capture / later sync to centralized management system
  • 26.
    Infinite Campus Education/ Social Interaction  Selectively push out learning modules / multimedia lessons to individual students  Teachers and students use custom mobile chat apps for real-time Q&A during lectures  Homework assignments can be completed offline anywhere
  • 27.
    Offline Sensor Data A weather balloon could collect sensor data while offline and sync it whenever it is online
  • 28.
    Travel Expense Tracker • I am in Tokyo, but my iPhone 5 is locked so I cannot use a local SIM card. • But I need an app to track my expenses, so I must be able to enter expenses while offline. • When I get back to the US, I want to sync these expenses to the Cloud so I can download a spreadsheet.
  • 29.
    Resources • Theseslides will be made available on SlideShare • Sample code links • https://github.com/couchbaselabs/ToDoLite-Android • https://github.com/couchbaselabs/ToDoLite-iOS • https://github.com/tleyden/office-radar • Documentation • http://developer.couchbase.com/mobile • Download - http://mobile.couchbase.com • Contact: couchbasejp@couchbase.com
  • 30.
    Resources 2 •Couchbase Mobile Google Group (community): groups.google.com/group/mobile-couchbase/ • Couchbase Mobile Twitter: https://twitter.com/CouchbaseMobile • Run Sync Gateway (and Couchbase Server!) under Docker: http://tleyden.github.io/blog/2014/06/22/running-couchbase- sync-gateway-on-gce/
  • 31.
    Upcoming Event: Hackathon • http://couchbasejpcommunity.doorkeeper.jp/events/13988
  • 32.
    Upcoming Event: CouchbaseServer • Couchbase Server http://couchbasejpcommunity.doorkeeper.jp/events/14785
  • 33.
    Thanks for watching! Contact: tleyden@couchbase.com Questions?