Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase data Structure for a twitter clone

Tags:

firebase

I'm trying to create a twitter clone to learn to use Firebase and I would love to get a suggestion about how to create the database structure. My biggest concern is related to followers and how to create a timeline when you are following for example 500 users. You would need to perform 500 queries and sort somehow for datetime.

{
  "followers" : {
    "cesar" : {
      "followers" : {
        "cesar2" : true
      },
      "following" : {
        "cesar2" : true
      }
    },
    "cesar2" : {
      "followers" : {
        "cesar" : true
      },
      "following" : {
        "cesar" : true
      }
    }
  },
  "tweet" : {
    "cesar" : [ null, {
      "content" : "tweet 1"
    } ]
  },
  "users" : {
    "cesar" : {
      "name" : "César",
      "notifications" : true,
      "username" : "cesar"
    },
    "cesar2" : {
      "name" : "César2",
      "notifications" : false,
      "username" : "cesar2"
    }
  }
}
like image 788
César Rodríguez Avatar asked Aug 01 '15 12:08

César Rodríguez


1 Answers

See Firefeed, Firebase's open-source Twitter clone. It includes a walkthrough of the data structure it uses, which boils down to using a fan-out approach when new messages are posted. Here's a copy of the rules used, which describes the underlying data structure:

{
  "rules": {
    // All data is readable by anyone.
    ".read": true,
    "people": {
      // A list of users with their names on the site.
      "$userid": {
        // Only the user can write their own entry into this list.
        ".write": "$userid ==auth.uid"
      }
    },
    "users": {
      "$userid": {
        // The user is allowed to write everything in their bucket.
        ".write": "$userid ==auth.uid",
        "following": {
          // The following list should only contain actual ids from the "people" list.
          "$followingid": {
            ".validate": "root.child('people').hasChild($followingid)"
          }
        },
        "followers": {
          // Anyone can add themself to to this user's followers list.
          "$followerid": {
            ".write": "$followerid ==auth.uid"
          }
        },
        "feed": {
          "$sparkid": {
            // User A can write in user B's feed, but only if A is following B, and only for sparks for which they are the author.
            ".write": "root.child('users/' + $userid + '/following').hasChild(auth.uid) && root.child('sparks/' + $sparkid + '/author').val() ==auth.uid"
          }
        }
      }
    },
    "sparks": {
      // A global list of sparks (the "firehose").
      "$sparkid": {
        // Modifying an existing spark is not allowed.
        ".write": "!data.exists()",
        // Every spark should have an author and a body.
        ".validate": "newData.hasChildren(['author', 'content'])",
        // A user can attribute a spark only to themselves.
        "author": {
          ".validate": "newData.val() ==auth.uid"
        },
        "content": {
          ".validate": "newData.isString()"
        }
      }
    },
    "recent-users": {
      // Users can add themselves to the list of users with recent activity.
      "$userid": {
        ".write": "$userid ==auth.uid"
      }
    },
    "recent-sparks": {
      // Authors of sparks can add their sparks to this list.
      "$sparkid": {
        ".write": "root.child('sparks/' + $sparkid + '/author').val() ==auth.uid"
      }
    },
    "search": {
      "firstName": {
        "$searchKey": {
          ".write": "auth != null && (root.child('people/' +auth.uid + '/firstName').val() + '|' + root.child('people/' +auth.uid + '/lastName').val() + '|' +auth.uid) == $searchKey && newData.val() ==auth.uid"
        }
      },
      "lastName": {
        "$searchKey": {
          ".write": "auth != null && (root.child('people/' +auth.uid + '/lastName').val() + '|' + root.child('people/' +auth.uid + '/firstName').val() + '|' +auth.uid) == $searchKey && newData.val() ==auth.uid"
        }
      }
    }
  }
}
like image 100
Rob DiMarco Avatar answered Oct 23 '22 01:10

Rob DiMarco