I have a page, the contents are gotten dynamically with php and Mysql.I use a foreach loop to display all the content.
foreach($data as $key){
echo "<div id="post_content"> $key['post'] </div>";
}
Now I get my feed just as it is, but the problem here is, the page needs to be refreshed before you see new post rows from the database.
This is where twitter comes in, when you’re on twitter and there’s a new post from someone you are following, it shows “1 new tweet” at the top of the feed, when you click this it adds the content of the tweet to the feed.
From my research, i’ve found a couple of ways to do this;
-
Websockets. Cons: Cross browser compatibility.
-
Using AJAX:
refresh = setInterval(function(), 5000);Cons: Well, loading a script every 5 second :/ - Someone recomended socket.io to me and i’m still looking at how to integrate it with php.
My questions is, is there any other effective(prefarably simple and effecient) way of doing this?
Any help would be greatly appreciated.
The problem is that anything socket related has a limit to the amount of connections opened at a particular point in time. So if you’re not planning to serve tens of thousands of concurrent duplex connections then you’re better off using a combination of AJAX and JSON with your PHP.
I recommend you implement polling intervals. eg, when a user isn’t active after a certain amount of time, increase the interval or assuming nothing new shows up for 5- 10 polls, increase the interval, perhaps doubling it every time until it reaches, let’s say, 2 mins. On getting a message or value back, reduce the intervals back to 5 seconds or less.