Supercharge your Firebase App with Google Cloud Platform

778425a9498f00198e57896c7b2a95d3?s=47 sararob
August 03, 2016

Supercharge your Firebase App with Google Cloud Platform

778425a9498f00198e57896c7b2a95d3?s=128

sararob

August 03, 2016
Tweet

Transcript

  1. APP DEVELOPMENT Supercharge your Firebase app with Google Cloud Platform

    Sara Robinson @SRobTweets
  2. 2 App Development @SRobTweets Who am I? Developer Advocate, Google

    Cloud Platform Sara Robinson / @SRobTweets • New York, NY • Harry Potter aficionado • Swift fan (Taylor and language)
  3. 3 App Development 3

  4. None
  5. Firebase at a glance

  6. That’s a lot of products! Let’s focus on four

  7. That’s a lot of products! Let’s focus on four Backed

    by Google Cloud Storage Export raw data to BigQuery
  8. Source: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis

    non erat sem Synchronize data across devices in milliseconds
  9. // Create a reference let databaseRef = FIRDatabase.database().referenceWithPath("messages") // Save

    data with setValue() databaseRef.setValue("Hello everyone!") // Sync data databaseRef.observeEventType(.Value, withBlock: { snapshot in print(snapshot.value) }) Store & Sync Data 9
  10. // Enable disk persistence FIRDatabase.database().persistenceEnabled = true // Specify data

    that should be pre-fetched and kept up to date databaseRef.keepSynced(true) 10 Handling Offline Data
  11. 11 App Development @SRobTweets Handling offline data: bit.ly/firebase-offline-data

  12. Source: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis

    non erat sem
  13. FIRAuth.auth()?.signInAnonymouslyWithCompletion({ (user, error) in if (error != nil) { print(error)

    } else { print(user.uid) } }) Anonymous Authentication 13
  14. 14 App Development @SRobTweets Getting Started on iOS - CocoaPods

    // Podfile pod 'Firebase/Database' pod 'Firebase/Auth' // Terminal $ pod install $ open your-project.xcworkspace // ViewController.swift import Firebase import FirebaseDatabase import FirebaseAuth
  15. 15 App Development 15 15 Live Demo #1

  16. 16 App Development 16 The app

  17. 17 App Development 17 Firebase Storage and Analytics 02 Firebase

    + Cloud Integrations
  18. Source: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis

    non erat sem Easily store and serve files and images, backed by Google Cloud Storage
  19. // Create a reference let storageRef = FIRStorage.storage().reference() let imageRef

    = storageRef.child("images/ocean.jpg") // Upload files from data in memory let data: NSData! = … imageRef.putData(data, metadata: nil) { (metadata, error) -> Void in if (error != nil) { // Uh-oh, an error occurred! } else { // Metadata contains data on size, content-type, and download URL. let downloadURL = metadata!.downloadURL } } Uploading Files 19
  20. // Anyone to read a public image if the file

    is less than 100kB // Anyone can upload a public file ending in '.txt' match /public/{imageId} { allow read: if resource.size < 100 * 1024; allow write: if imageId.matches(".*\.txt"); } // Require authentication on all internal image reads match /internal/{imageId} { allow read: if request.auth != null; } Storage Security Rules 20
  21. Free and unlimited analytics, see user behavior from a single

    dashboard
  22. 22 App Development @SRobTweets Analytics Dashboard - User Engagement Android

    iOS
  23. 23 App Development @SRobTweets Analytics Dashboard - Device Type Android

    iOS
  24. 24 App Development @SRobTweets Analytics Dashboard - User Demographics Android

    iOS
  25. 25 App Development @SRobTweets Custom Events Dashboard

  26. 26 App Development 26 26 Firebase Analytics => BigQuery Export

  27. BigQuery • Fast: terabytes in seconds • Simple: SQL •

    Scaleable: From bytes to petabytes • Interoperable: Java, Python, Tableau, R... • Pay only for what you use • Free monthly quota - 1 TB 27
  28. 28 App Development @SRobTweets Firebase Analytics + BigQuery Export your

    Firebase Analytics data to BigQuery for custom analysis Android data iOS data
  29. // Create an analytics event FIRAnalytics.logEventWithName("pastryPurchased", parameters: [ "item_type": "cannoli",

    kFIRParameterValue: "3.5" ] ) Creating an Analytics Event 29
  30. 30 App Development 30 30 Live Demo #2

  31. 31 App Development 31 31 We’ll build....

  32. 32 App Development 32 32 A photo storage app with

    image detection
  33. The app Cloud Vision API

  34. None
  35. Cloud Vision API Request { "requests":[ { "image": { "source":

    { "gcs_image_uri": "gs://images/your-photo-uri" } }, "features": [ { "type": "LABEL_DETECTION", "maxResults": 10 }, { "type": "FACE_DETECTION", "maxResults": 10 }, // More feature detection types... ] } ] }
  36. 36 App Development @SRobTweets How will we store our data?

    User UID Image ID Vision API response object
  37. 37 App Development @SRobTweets CocoaPods Quickstart // Podfile pod 'Firebase/Storage'

    pod 'Firebase/Analytics' // ViewController.swift import Firebase import FirebaseStorage import FirebaseAnalytics
  38. // Create a path to upload to Firebase Storage let

    imagesRef = FIRStorage.storage().referenceForURL("gs://swift-storage.appspot.com").child("imag es/\(userUid)/\(imageId).\(fileExtension)") // Upload to Firebase storage imagesRef.putData(imageData, metadata: imgMetadata) { (metadata, error) -> Void in if (error == nil) { FIRAnalytics.logEventWithName("imgUploadSuccess", parameters: [ "content_type": fileExtension, ]) let gcsUri = imagesRef.description self.createRequest(gcsUri) // Send the image to the Vision API } } Step 1: Upload images to Firebase Storage 38
  39. // Get the photo metadata we want to save to

    our Firebase Database let visionApiResponse: AnyObject = responses.rawValue let imageDataforFB = [ "visionResponse": dictResponses, "gcsUri": imgRef ] // Get a unique Firebase Database ref for the image and save it let userImageRef = FIRDatabase.database().referenceWithPath("\(userUid)/images") let uniqueImageRef = userImageRef.childByAutoId() uniqueImageRef.setValue(imageDataforFB) Step 2: Save photo metadata to the Firebase Database 39
  40. // Listen for items added to our database at /user/images/

    userImageRef.observeEventType(.ChildAdded, withBlock: { snapshot in // Get the Firebase Storage reference for the latest image added let imageUrl = snapshot.value?.objectForKey("gcsUri") as! String let storageRef = FIRStorage.storage().referenceForURL(imageUrl) // Download the image as a URL and populate our UICollectionView storageRef.downloadURLWithCompletion({ (url, error) in var currentPhoto = PhotoHolder() currentPhoto.firebaseKey = snapshot.key currentPhoto.url = url self.photoArray.append(currentPhoto) self.collectionView?.reloadData() }) }) Step 3: Listen for new images and add them to the view 40
  41. Google Cloud Platform ‹#› Google Cloud Platform Compute Connectivity Big

    Data & ML Storage Developer Tools Mobile Management
  42. 42 App Development @SRobTweets Resources Firebase docs: firebase.google.com/docs [Video] Build

    a cross-platform Firebase app! bit.ly/zero-to-app Vision API samples: github.com/GoogleCloudPlatform/cloud-vision Join the discussion bit.ly/gcp-slack bit.ly/gcp-github bit.ly/gcp-twitter
  43. 43 App Development 43 Thank You Sara Robinson / @SRobTweets