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

Firebase Realtime Database

Soham Mondal
November 09, 2016

Firebase Realtime Database

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

Soham Mondal

November 09, 2016
Tweet

More Decks by Soham Mondal

Other Decks in Technology

Transcript

  1. Firebase Realtime Database
    Soham Mondal

    View full-size slide

  2. @s0h4m
    [email protected]
    — Founder, Triveous
    — Creator, Voice Recorder
    — Google Expert, UX
    — Android/UX consultant
    — Organizer, Blrdroid
    — Mentor, Google Launchpad Accelerator
    Soham Mondal

    View full-size slide

  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

    View full-size slide

  4. Firebase
    Mobile and web
    applications platform
    http://firebase.google.com

    View full-size slide

  5. Firebase
    Realtime Database

    View full-size slide

  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

    View full-size slide

  7. Realtime
    features

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  10. — Server
    — Database
    — Realtime Updates
    — Synchronizing state
    — Client and server libraries
    — Offline
    The Realtime Collaborative Movie App

    View full-size slide

  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

    View full-size slide

  12. Getting started
    Firebase Realtime Database

    View full-size slide

  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'

    View full-size slide

  14. FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("message");
    myRef.setValue("Hello, Movie Booking app!");

    View full-size slide

  15. Read Data
    Value and Child Event Listeners

    View full-size slide

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

    View full-size slide

  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) {
    }
    });

    View full-size slide

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

    View full-size slide

  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);

    View full-size slide

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

    View full-size slide

  21. Offline
    Read and Write data

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  24. DatabaseReference chatRef = FirebaseDatabase.getInstance()
    .getReference(”chat");
    chatRef.keepSynced(true);
    scoresRef.orderByValue().limitToLast(4)
    .addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot snapshot, String previousChild) {
    }
    });

    View full-size slide

  25. Structuring Data
    SQL vs NoSQL

    View full-size slide

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

    View full-size slide

  27. {
    "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": { ... }
    }
    }

    View full-size slide

  28. {
    "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,

    View full-size slide

  29. "two": { ... },
    "three": { ... }
    },
    "messages": {
    "one": {
    "m1": {
    "name": ”soham",
    "message": ”ok what was that movie again",
    "timestamp": 1459361875337
    },
    "m2": { ... },
    "m3": { ... }
    },
    "two": { ... },
    "three": { ... }
    }
    }

    View full-size slide

  30. Does it sync in the background?
    32

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  34. Caveats
    — Good but inflexible API
    — Search Query
    — Server side code
    — Migration
    — Limited use case
    — Play Services
    — Hosted

    View full-size slide

  35. Design patterns
    Architecture

    View full-size slide

  36. Auxilliary
    Auxilliary
    Customer support
    Troubleshooting
    Core
    Core Features
    Chat application
    Cab booking service
    Module
    Modular Features
    Chat
    Polls, interaction

    View full-size slide

  37. Alternatives

    View full-size slide

  38. Very popular with developers
    Launched recently
    Good offline db
    Excellent API
    Realm
    Open souce
    Reliable
    Android solution?
    Couch
    RethinkDB
    Gun
    Others
    Alternatives

    View full-size slide

  39. Altenatives
    Realm Mobile Platform
    Object database
    Native models
    Easy to use
    Great client side
    performance
    Better offline
    experience
    Built in
    encryption
    Advantages

    View full-size slide

  40. Thank You
    Questions?
    @s0h4m

    View full-size slide

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

    View full-size slide