22
Couchbase Mobile Developing Mobile Apps & Offline Experiences with Couchbase Mobile 1

Couchbase Mobile - Meetupfiles.meetup.com/13875572/Couchbase Mobile - mobile_tea_Boston_0… · Couchbase Mobile Developing Mobile Apps & Offline Experiences with Couchbase Mobile

  • Upload
    lekhanh

  • View
    232

  • Download
    0

Embed Size (px)

Citation preview

Couchbase Mobile Developing Mobile Apps & Offline Experiences with Couchbase Mobile

1

About Me

William Hoang Mobile Developer Advocate @sweetiewill

2

Couchbase  Lite Sync  Gateway Couchbase  Server    

Couchbase  Lite   Sync  Gateway  

Couchbase  Lite

Features  &  Benefits:     §  Document-­‐Oriented  and  Schema-­‐Less  

§  Full  Document  Index,  Query,  Sync  Capabilities  

§  Advanced  Filter  and  Replication  Features  

§  Peer-­‐to-­‐Peer  Replication  

§  Conflict  Resolution  

©2015  Couchbase  Inc.   ‹#›  

Session  Overview  

▪ What  is  peer-­‐to-­‐peer?  ▪ P2P  with  Couchbase  Mobile’s  replication  architecture  ▪ Implementing  peer-­‐to-­‐peer  ▪ Using  Bonjour  (aka  mDNS,  Network  Service  Discovery…)  ▪ Direct  pairing  via  QR-­‐code  scanning  ▪ Demos  

7

Don’t&drain&all&my&battery!Decentralised,Web

©2014&Couchbase&Inc.

P2P

�26

becomes

©2015  Couchbase  Inc.   ‹#›  

Benefits  Of  Peer-­‐To-­‐Peer  

▪ Developer  doesn’t  need  to  run  servers  ▪ End-­‐user  privacy  ▪ Unmetered  high  bandwidth  connections  ▪ Useable  with  no  Internet  infrastructure  ▪ Cellular  “dead  zones”  ▪ Wilderness  ▪ Disaster  areas  

9  

Don’t&drain&all&my&battery!Decentralised,Web

©2014&Couchbase&Inc.

Mesh%Networks

�30

Couchbase  Lite   Sync  Gateway  

Sync  Gateway

Features  &  Benefits:     §  Less  Code  with  Sync  API  and  Custom  Configurations  

§  Custom  Data  Flow  and  Control  of  Data  Routing    

§  Authentication  and  Authorization  

§  Change  Validation,  Access  Control  

§  Scalable  

©2015  Couchbase  Inc.   12  

Implementation  

§ Uses  a  QR  code  for  peer  discovery  § Uses  one-­‐time  push  replication  to  transfer  photos  § Uses  digest  authentication  § Generates  one-­‐time  user  credentials  § Supports  multiple  platforms  (e.g.,  iOS  and  Android)  

©2015  Couchbase  Inc.   13  

Storyboard  ViewController! SendViewController!

ReceiveViewController!

QR Code Scanner!

Presents a QR code!

©2015  Couchbase  Inc.   14  

ReceiveViewController  Step  1:  Start  a  listener  

listener  =  CBLListener(manager:  CBLManager.sharedInstance(),  port:  0)    listener.requiresAuth  =  true  let  username  =  secureGenerateKey(NSCharacterSet.URLUserAllowedCharacterSet())  let  password  =  secureGenerateKey(NSCharacterSet.URLPasswordAllowedCharacterSet())  listener.setPasswords([username  :  password])    var  success  =  listener.start(&error)  if  (success)  {  

syncUrl  =  genearteSyncURL(listener.URL,  username:  username,  password:  password,    

dbName:  database.name)    

startObserveDatabaseChange()  }  

©2015  Couchbase  Inc.   15  

ReceiveViewController  Step  2:  Observe  database  changes  

func  startObserveDatabaseChange()  {  NSNotificationCenter.defaultCenter().addObserverForName(  kCBLDatabaseChangeNotification,  object:  database,  queue:  nil)  {  (notification)  -­‐>  Void  in        if  let  changes  =  notification.userInfo!["changes"]  as?  [CBLDatabaseChange]  {              for  change  in  changes  {  

dispatch_async(dispatch_get_main_queue(),  {  self.saveImageFromDocument(change.documentID)  

                     })                    }              }        }  }  

©2015  Couchbase  Inc.   16  

ReceiveViewController  Step  3:  Generate  and  display  a  QR  code  

http://user:password@<listener  host>:<port>/db  

Uses  the  iOS  Core  Image  filter    

©2015  Couchbase  Inc.   17  

SendViewController  Step  1:  Scan  a  QR  code  

Uses  the  iOS  class  AVCaptureSession  with    the  output  type  AVMetadataObjectTypeQRCode  

©2015  Couchbase  Inc.   18  

ReceiveViewController  Step  2:  Create  documents  

var  docIds:  [String]  =  []  for  asset  in  sharedAssets!  {  

//...  var  data  =  NSData(bytesNoCopy:  imageBytes,  length:  buffered,  freeWhenDone:  true)    let  doc  =  database.createDocument()  let  rev  =  doc.newRevision()  rev.setAttachmentNamed("photo",  withContentType:  "application/octet-­‐stream",  content:  

data)  let  saved  =  rev.save(&error)    if  saved  !=  nil  {  

docIds.append(doc.documentID)  }  

}  

©2015  Couchbase  Inc.   19  

ReceiveViewController  Step  3:  Replicate  documents  

replicator  =  database.createPushReplication(url)  replicator.documentIDs  =  docIds    NSNotificationCenter.defaultCenter().addObserverForName(kCBLReplicationChangeNotification,  object:  replicator,  queue:  nil)  {  (notification)  -­‐>  Void  in  

if  self.replicator.lastError  ==  nil  {  var  totalCount  =  self.replicator.changesCount  var  completedCount  =  self.replicator.completedChangesCount  if  completedCount  >  0  &&  completedCount  ==  totalCount  {  

self.statusLabel.text  =  "Sending  Completed”  }  

}  else  {  self.statusLabel.text  =  "Sending  Abort"  

}  }    replicator.start()  

©2015  Couchbase  Inc.   20  

Couchbase  Mobile  Replication  

§ Based  on  CouchDB*  architecture  § Designed  from  the  start  for  arbitrary  topologies  

 *Not  the  same  as  Couchbase  Server’s  XDCR  

Replicator   REST  API  

Active   Passive  

Couchbase  Mobile Demo

Thank  you.