title: How to model a timeline that automatically adds post from every follower on firebase database
body: <p>I am building a social app, I have a database on firebase that is structured like this:</p>
  <pre><code>tJWRbfqUUbSQn5eI8ZS5vPG9znr1
  
    bio:
  
    coverPhotoUrl:
  
    email:
  
    firstname:
  
    imgUrl:
  
    lastname:
  
    name:
  
    phone:
  
    posts
  
      -KPN0b7QmNp8AjYy4yFl
      -KPXmtvZdiQ--QjZ-j3m
      -KPc7PpDkmFNU9JjlIJP
      -KPc7PptOulQdrpsrGEp
      -KPc7Pq6JC7bjt8-sRFU
      -KPc7PqJ2651EIQWXIv6
  </code></pre>
  <p>the top level key is th user id, so i have that for every user, each user has posts too, inside the posts node, i have comments, pushed in the exact the same way as posts.
  My question is that I am a bit curious about how i am going to model a Newsfeed, right now A Newsfeed to me is a list of posts from you and your everyone you are following in the exact order of the time they were posted while a timeline is just posts from the said user in that order too.</p>
  <p>I have correctly done the timeline by just iterating over posts from the user
  I have tried to do the Newsfeed part in two ways, which has bugs and I think is not very efficient</p>
  <ol>
  <li><p>Since every user maintains a list of his followers in his own node, i made it such that anytime a user makes a post, he not only writes in his own 'posts node' but also in every other person following him's 'Newsfeed' node</p>
  <p>Cons of this method
  a. in the event of an unfollow, i am not able to remove user A's posts from User B's timeline
  b. i am giving every user access to write in every other person's node, which as i have read, is not a very good practice.</p>
  </li>
  <li><p>Maintain the url of User A's post node in user B's Timeline node, which would solve the con B of method 1.</p>
  </li>
  </ol>
  <p>Cons of this method
  a. I dont know how to implement it.</p>
  <p>I dont know if there is any textbook way to do this(of course there has to be, there is only so much social network platforms out there), if there is i want to know, also if you could point me to how(not help me to) implement my method 2, i will very much appreciate it.</p>
  <p>Also, i was following  <a href="https://stackoverflow.com/questions/33593378/firebase-newest-feed-for-following-users">this similar question</a></p>
  <p>which does not completely answer my question but looks like a very interesting approach</p>