The way twitter do it is basically:
- Have a regular AJAX call to a server-side script that returns the number of new tweets
- When this number is greater than 0, the "N new tweets" bar is shown
- Clicking this bar loads the new tweets via another ajax call
One way of catching how many updates there are is to keep track of the last tweet id shown on the user's page (each tweet has a unique id). So, when the page is loaded, the max(tweet id) is, say, 100. Your ajax call to check for updates will be something like:
/check_updates.php?last_seen=100
The check_updates.php
script then basically checks the DB for latest tweets for the logged in user where id > 100. Similarly, when the user clicks "Show tweets", the url called will be something along the lines of:
/show_updates.php?last_seen=100
This will mean that show_updates.php
only returns tweets that haven't been seen before, keeping response times to a minimum.
The examples above send the id of the last tweet back to the server for comparison, but it could equally be done via the current timestamp. The server-side calculations would essentially be the same (look for tweets since time: X rather than tweet id: y) but it has the benefit of the client side not having to keep track of the last tweet id loaded.