1

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;

  1. Websockets. Cons: Cross browser compatibility.

  2. Using AJAX: refresh = setInterval(function(), 5000); Cons: Well, loading a script every 5 second :/

  3. 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.

Saff
  • 563
  • 1
  • 10
  • 21
  • You don't want to set up a socket with every connection you may have. I'd do AJAX. You can improve your script by having it not make update calls to the server after a certain amount of inactivity by the user. This will save your server from being hit by inactive users. – Boundless Jan 12 '13 at 13:46
  • But the page is basically my home page, even for the active users wouldn't it still be inefficient? – Saff Jan 12 '13 at 13:53
  • If you use sockets you will greatly limit the amount of simultaneous users that your site can have. There is a hard cap on how many sockets can be open, and your server must dedicate resources to keeping these sockets open. – Boundless Jan 12 '13 at 14:01
  • Oh i get you, what do you think of node.js and socket.io? Would it fall under the same cat? – Saff Jan 12 '13 at 14:03
  • I have never used node.js. The bottom line is that you will be limited if you go any route that uses sockets. Maybe you're fine with putting a hard limit on the amount of simultaneous users to your site. If that's the case, pick a solution that uses sockets. Also check out this thread: http://stackoverflow.com/questions/1575453/how-many-socket-connections-can-a-typical-server-handle – Boundless Jan 12 '13 at 14:15
  • Many thanks @Boundless :) Nah i think i'll go with thw AJAX option haha – Saff Jan 12 '13 at 14:19
  • You should. If it is not important for your users to get real time immediate updates, it is pointless to use sockets. a 5 second delay should work just fine for things like new updates and it saves you a lot of resources. – Nutty Nur Jan 12 '13 at 17:04

2 Answers2

2

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.

Tommy Adey
  • 806
  • 6
  • 12
1

The easiest way to do this would be providing some kind of API so you can access the new "tweets" in a JSON format and then prepend them to your HTML.
socket.io would also be a good option as it combines multiple technologies like websockets, long polling etc. and degrades nicely when using older browsers. You can find many tutorials on socket.io online. However socket.io relies on node.js server side JS, so if you're not familiar with that this could be a bit complicated. Have a look at this link though: Using PHP with Socket.io

Community
  • 1
  • 1
Stefan
  • 2,164
  • 1
  • 23
  • 40
  • Thanks, this just makes me admit to myself that i have to learn node.js :( – Saff Jan 12 '13 at 13:55
  • You can still go for a PHP only solution by providing a JSON API and then query it every X seconds via AJAX – Stefan Jan 12 '13 at 13:56
  • Yh i thought about that but i'm guessing that would be inefficient. How do i know my hosting account allows me to use node.js and could you recommend a good tutorial? Thanks @stefan – Saff Jan 12 '13 at 13:59
  • Most hosting companies don't support node.js, just email them and ask. You can find hundreds of tutorials through google – Stefan Jan 12 '13 at 14:01
  • Ha another setback, looks like i'll stick with AJAX for now haha, Thanks for the help @Stefan, do you use node.js yourself? – Saff Jan 12 '13 at 14:05