How can i impelement a Facebook NewsFeed like feature in my PHP based community application? For example "X user uploaded a new photo, Z attending to a event". Fetching all this data from different tables consumes too resources.
Thanks.
There are a couple of different ways you can do this, but the one I would suggest is this. Keep a single 'history' table that holds all of the records for every user, a timestamp, and a description of the action along with flags (or predefined keys) for what type of action occurred. This would allow you to query a single table selectively for certain types of actions and within a set frame of time to display. However, with a lot of users this table will grow quickly so you may consider purging old data that you know you will not use again later. Pay close attention to indexes to keep the queries fast, and consider using query caching since you will probably be generating the same results frequently.
As long as you aren't planning on using this historical data for auditing users actions then it should be fine to dump it after a while. If you do need an audit trail then you may need to consider creating a temp_history table for showing recent actions and then in a CRON move their recent actions over to a permanent audit table for long term storage.
Start by defining what you call activity on your website. A activity can contain multiple hyper links. So a php multidimensional array might help in here which can go in the table as serialized object.
Say x is now connected with y, z, a and b. you can define y, z, a and b as profile links. Say x commented on y new photo. In this case y become a new hyper link to the person's profile photo. Say x became a fan of zoozoo group. In this case zoozoo becomes a hyper link to the fan page.
Create a activity table, to scale it large make it activity_USER_ID, every time a user registers you create this table and check that you don't have to create it once again.
Define Activities as in 1,2,3 or whatever you feel is the best activity that people might be interested in communities you support.
table structure should be simple : activity php array, timestamp. If you are using jQuery and Ajax to populate the activity like done in facebook with mouse scroll then you can always convert the php array to json and format it on the client side.
Do this whenever user logs in.
If the user base is large say a million then you might want to put in some job processing system which will put events in event_MM_DD_YYYY and you can then do a batch processing of those events to store it in the activity table.
Never display more than 10 20 30 recent activities.
Hope this helps for you :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With