Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Firebase Realtime Database

Ad94879495e21b7362d0d2c845f44bc0?s=47 Soham Mondal
November 09, 2016

Firebase Realtime Database

Slides from my talk on Firebase Realtime Database at Droidcon India 2016

Ad94879495e21b7362d0d2c845f44bc0?s=128

Soham Mondal

November 09, 2016
Tweet

Transcript

  1. Firebase Realtime Database Soham Mondal

  2. @s0h4m soham@triveous.com — Founder, Triveous — Creator, Voice Recorder —

    Google Expert, UX — Android/UX consultant — Organizer, Blrdroid — Mentor, Google Launchpad Accelerator Soham Mondal
  3. Firebase Realtime Database Agenda Introduction API Caveats and Alternatives —

    Introduction to the Firebase platform — Firebase Realtime Database — Realtime challenges — Use Case — Feature set — Getting Started — Read — Write — Value Event Listeners vs Child Event Listeners — Offline — Data structure — Query — Realtime + Admin API — Design patterns — Alternatives — Realm Mobile Platform/CouchDB/Others
  4. Firebase Mobile and web applications platform http://firebase.google.com

  5. Firebase Realtime Database

  6. Book movies and pay onling Book Browse the latest movies

    Browse People should not stop using the app and keep using it do bookings Retention Increase engagement in consumers so they spend more time in the app Engagement
  7. Realtime features

  8. Realtime Chat Talk to your friends and discuss the latest

    movies Realtime polling Poll on viewing the latest movies, the theatre, timing or even genre
  9. Updated Movie App Higher Engagement Retention Competitive Edge Benefits Book

    movies and pay onling Book Browse the latest movies Browse Poll on movies, shows and timings Poll Talk about the latest movies Talk
  10. — Server — Database — Realtime Updates — Synchronizing state

    — Client and server libraries — Offline The Realtime Collaborative Movie App
  11. Firebase Realtime Database — Hosted Json Database — Realtime Updates

    — Google Cloud — Scalable — Consistency — Offline — Speed — Good pricing — Cross platform support — Admin API — Firebase UI 11
  12. Getting started Firebase Realtime Database

  13. apply plugin: 'com.android.application' dependencies { // ... compile 'com.google.firebase:firebase-core:9.8.0' compile

    'com.google.firebase:firebase-database:9.8.0' } apply plugin: 'com.google.gms.google-services'
  14. FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference myRef = database.getReference("message"); myRef.setValue("Hello, Movie

    Booking app!");
  15. Read Data Value and Child Event Listeners

  16. FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference myRef = database.getReference("message");

  17. FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference myRef = database.getReference("message"); myRef.addValueEventListener(new ValueEventListener()

    { @Override public void onDataChange(DataSnapshot dataSnapshot) { } @Override public void onCancelled(DatabaseError error) { } });
  18. FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference myRef = database.getReference("message"); myRef.addListenerForSingleValueEvent()

  19. ChildEventListener childEventListener = new ChildEventListener() { @Override public void onChildAdded(DataSnapshot

    dataSnapshot, String previousChildName) {} @Override public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {} @Override public void onChildRemoved(DataSnapshot dataSnapshot) { } @Override public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {} @Override public void onCancelled(DatabaseError databaseError) {} }; myRef.addChildEventListener(childEventListener);
  20. Query

  21. Query chatMessages = databaseReference.child(”chats") .orderByChild(”timestamp"); chatMessages.addChildEventListener(new ChildEventListener() { });

  22. Offline Read and Write data

  23. FirebaseDatabase.getInstance().setPersistenceEnabled(true);

  24. DatabaseReference chatRef = FirebaseDatabase.getInstance() .getReference(”chat"); chatRef.keepSynced(true);

  25. DatabaseReference chatRef = FirebaseDatabase.getInstance() .getReference(”chat"); chatRef.keepSynced(true); scoresRef.orderByValue().limitToLast(4) .addChildEventListener(new ChildEventListener() {

    @Override public void onChildAdded(DataSnapshot snapshot, String previousChild) { } });
  26. Caveats

  27. Structuring Data SQL vs NoSQL

  28. https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/

  29. { "chats": { "one": { "title": ”Soham Mondal", "messages": {

    "m1": { "sender": ”Soham", " Yes, let us definitely go ahead and book this otherwise…" }, "m2": { ... }, // a very long list of messages } }, "two": { ... } } }
  30. { "chats": { "one": { "title": ”Soham Mondal", "lastMessage": ”Yes,

    let us definitely go ahead and book this otherwise…", ”imageUrl": ”...", "timestamp": 1459361875666 }, "two": { ... }, "three": { ... } }, "members": { "one": { ”nidhi": true,
  31. "two": { ... }, "three": { ... } }, "messages":

    { "one": { "m1": { "name": ”soham", "message": ”ok what was that movie again", "timestamp": 1459361875337 }, "m2": { ... }, "m3": { ... } }, "two": { ... }, "three": { ... } } }
  32. Does it sync in the background? 32

  33. START 2:15 pm App Open Send Message App goes into

    the background 3:00 pm App open New Message
  34. START 2:15 pm App Open Send Message App goes into

    the background 3:00 pm App open New Message
  35. START 2:15 pm App Open Send Message App goes into

    the background 3:00 pm App open New Message
  36. Admin SDK

  37. None
  38. Server sdk

  39. Caveats — Good but inflexible API — Search Query —

    Server side code — Migration — Limited use case — Play Services — Hosted
  40. Design patterns Architecture

  41. Auxilliary Auxilliary Customer support Troubleshooting Core Core Features Chat application

    Cab booking service Module Modular Features Chat Polls, interaction
  42. Alternatives

  43. Very popular with developers Launched recently Good offline db Excellent

    API Realm Open souce Reliable Android solution? Couch RethinkDB Gun Others Alternatives
  44. Altenatives Realm Mobile Platform Object database Native models Easy to

    use Great client side performance Better offline experience Built in encryption Advantages
  45. Thank You Questions? @s0h4m

  46. Links: https://firebase.google.com/ https://highlyscalable.wordpress.com/2 012/03/01/nosql-data-modeling- techniques/ https://realm.io/