Reputation: 1433
BACKGROUND
Im relatively new to programming without any formal training and/or schoooling and am trying to pick things up as i go. I am currently trying to code a simple facebook-esque news feed where users are split into 2 general groups, the ones being followed(A), and the ones following(B).
I plan on making the feed one directional[i.e B follows A(and gets updates/notifications from A) BUT A does NOT follow B back] in order to keep it simple.Furthermore, whenever A does something, i.e a status update, i plan on updating the feeds of B with A's activity.
I've done a bit of reading both here on SO and some googling on my own but have met 2 problem, the first being that most of what i've seen is over my head (using tools like redis etc), and the second being that most articles and answers i've read addresses the issue of what to do(message queuing, push/pull) but does not address the issue of HOW to actually go about doing it.
I plan on having a table(follow-info) that stores info on how is following whom:
---------------
| id | A | B |
---------------
| 1 | 3 | 5 |
----------------
| 2 | 3 | 11| WHERE A= id of people being followed AND B=id of people following.
----------------
| 3 | 7 | 2 |
----------------
| 4 | 3 | 1 |
---------------
and other tables which track his activity
-----------------------------------------
| id | activities | last-update |
-----------------------------------------
| 1 | details | 2013-12-8 00:00:00 | WHERE last-update = ON UPDATE CURRENT
----------------------------------------- TIMESTAMP
Therefore whenever the last-update time changes(i.e an activity occurs), i would like to post the details of the activity to B(the one following).
QUESTION
How exactly would i go about posting updates on someone else's "wall"? I know that i would have to query the database for the r/s between users(i.e who is following whom)but how do i then push the activity details on the user's walls?Most of what i've read seems to suggest setting up a timer of sorts which would query the server for any updates(exactly how i would go about doing it was never exactly mentioned.) but in my limited knowledge, wouldn't that be quite a waste of resources?(Why keep needlessly querying the server when there might be no updates when you can simply roll em out as they occur?)
P.S i know basic html,php and jquery and am currently running using PHP 5.3 and Apache(uniform server).
Any advice/help and/or opinions would be appreciated.
Upvotes: 0
Views: 2015
Reputation: 3187
What you are trying to achieve might look simple at first, but by looking at the BIG Picture there are many things sticking their noses in your business if you are trying to simply simulate the problem then it should be no big problem but if you are having a go for production purposes and you are about to target million of uses then you need teamwork and a lot of teamwork.
Suggested Solution just for simulation purpose
Database Design
Your database should be simple having 3 Tables 1 Users
(following or followed both), Table 2 Relations
(Who is following who). Table 3 Updates
(Who is sharing what). This is minimalist design.
Dynamic No-live News Feed
The feed here under discussion is to be considered dynamic but not live. First identify your user by logging him/her in and activating a session for him/her. Render your layout using css/css3, HTML/HTML5 and jQuery/Javascript, Now using server scripting (PHP/JSP/ASP.Net etc) query your database to 1. select all user being followed by logged user from Relations Table. Next select all updates from updates table for each user for which there exist a relation between (A. User who is owning the update B. User who is logged in identified by session). Now that you have your updates display your dynamic content to the Actively Logged User.
To Make it Live Feed
The Important part of your question was how to push updates to users. Well web is not interrupt driven instead client will have to ask for new data each time that's the reason AJAX requests exist in first place. Think if the Server had to push updates to each and every client when ready How would the server know that client is still online, or client has enough bandwidth to receive data, or the Client is not busy and many other concerns. The idea is to share load to some extent while making possible for Server to manage things plus HTTP protocol is state-less and is unable to achieve such functionality.
The Solution is Polling you use JavaScript SetTimeOut()
method to dispatch an AJAX request to Server and bring back updates if there is any and then update the user interface accordingly.
One strategy to reduce number AJAX calls to server is to use differential timing intervals for setTimeOut()
method. For instance you issued an AJAX request to Server and server responded with no updates then after 1sec you issued another AJAX request and again there were no updates now it is reason to issue next AJAX request after 2sec, next time 4sec... untill you get an update, in that case you might wanna reset your timer or use half interval of current one depending on your needs and strategy.
Even if you monitor you network activity in chrome browser or firefox you'll see that Facebook actively dispatches AJAX request and brings back online/offline statues of your friends/chat message/feed entries etc.
Upvotes: 4