Design a scalable feed model based on Firebase

Question:

How to create a social network "feed" with Firebase as a backend that scales?

Possible answers:

"MVP" is the design of the root child feeds, one for each user, and the addition of a new message from the next user in each subscriber channel.

users
  user1
    name: bob
  user2
    name: alice
    follows: 
      user1: true

posts
  post1
     author: user1
     text: 'Hi there'

feeds
  user2
    post1: true

This works well and is demonstrated in the project Firefeed. But this is not very good: if Katy Perry wants to publish something, her mobile phone will have to write millions of feeds.

Therefore, the solution reported in this SO question passed this operation to the server process.

, Firebase - "-backend" , , , , .

, feeds ?

:

baseRef.child('posts')
       .orderBy('author')
       .whereIn(baseRef.child('users/user2/follows').keys())

, whereIn Firebase API, : (

?

+4
1

Firebase : https://www.firebase.com/blog/2015-10-07-how-to-keep-your-data-consistent.html

" " ( ).

:

  • fannout ( , API)

    function fanoutPost({ uid, followersSnaphot, post }) {
            // Turn the hash of followers to an array of each id as the string
            var followers = Object.keys(followersSnaphot.val());
            var fanoutObj = {};
            // write to each follower timeline
            followers.forEach((key) => fanoutObj['/timeline/' + key] = post);
            return fanoutObj;  
    }
    
  • , :

    var followersRef = new Firebase('https://<YOUR-FIREBASE-APP>.firebaseio.com/followers');
    var followers = {};
    followersRef.on('value', (snap) => followers = snap.val());
    var btnAddPost = document.getElementById('btnAddPost');
    var txtPostTitle = document.getElementById('txtPostTitle');
    btnAddPost.addEventListener(() => {
          // make post
          var post = { title: txtPostTitle.value };
          // make fanout-object
          var fanoutObj = fanoutPost({ 
              uid: followersRef.getAuth().uid, 
              followers: followers, 
              post: post 
          });
          // Send the object to the Firebase db for fan-out
          rootRef.update(fanoutObj);
    });
    

. , , . , , . , . , , . ( , )

+5

Source: https://habr.com/ru/post/1599543/


All Articles