Couchbase Mobile Ideathon
Traun Leyden
Lead Android Developer
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 isslow 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
{ "_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"
}
Each Geofence Event is associated with an organization
OfficeRadar UserProfile Document
{ "_id":"10152586863333982","_rev":"1-2b6a6e182ef77cd15db7171023931e8d","type":"profile","authSystem":"facebook","name":"Traun Leyden","organization":"couchbase"
}
Each User Profile is associated with an organization
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
• 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
Putting the “No” in NoSQL
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
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
Picsolve
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: [email protected]
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