Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I am currently developing some plugins for WordPress, and I was wondering which route to take. Let's take an example, you want to display the last 3 tweets on your page.

Option 1

You do things the normal way inside WordPress. Someone enters the website, while generating the page, you fetch the tweets in php via the twitter api, and just display them where you want.

Now the small problem with this is, that you have to wait for the response from twitter. This takes a few ms. NO real problem, but this is question is just out of curiosity.

Option 2

Here you don't do anything in WordPress on the initial load, but you do have the API inside. Now you just generate the page, and as soon as the page is done on the client side, you do a small AJAX call back to the server via a WordPress plugin, to fetch your latest tweets. Also called asynchronously.

Now the problem with this IMO is that you have much more stress on your server. For starters you have two HTTP requests instead of one. Secondly the WordPress core has to load two times instead of one.

Other options

Now I know there are a lot of other options: 1) Getting the tweets directly via javascript, no stress on the server at all. 2) Cache the tweets so they are fetched from the DB instead of using the API every time. 3) Getting the tweets from an ajax call that is not a WordPress plugin. 4) Many more.

My Question

Now my question is if you only compare 1 and 2, which would be a better choice.

NOTE: I am only interested in comparing 1 and 2. No other options. The plugin I am creating has nothing to do with tweets, it's just an example to illustrate the problem.

share|improve this question
    
better choice from which point of view? Server load or user experience? –  Muad'Dib Nov 12 '11 at 17:39
    
Firstly in point of view of the server I guess. But I guess if the server slows down, this will have it's impact on the UX as well. –  Saif Bechan Nov 12 '11 at 18:15
add comment

2 Answers

up vote 6 down vote accepted

I would certainly recommend caching the tweets in your database or a plain text file. As soon as you start getting a lot of traffic you will be creating many hits to the Twitter API and going over their limit. And since you're not tweeting every 30 seconds day and night (I hope!) you could just end up fetching the same thing over and over.

In terms of updating the tweet cache, you could check on each page load whether it's more than X minutes since the tweets were last cached and if so update the cache. A few ms added to page load once every 30 minutes or hour isn't a problem.

In my experience, the perfect solution is to create a separate script that does the fetch & cache, and run this on a cron job by loading the URL. However you would need a dedicated server for this (or your own machine left on permanently).

EDIT: to answer your comment, if you really can only choose between options 1 and 2, then personally I would go for option 2 so that user load times are shorter. One extra HTTP request isn't a huge deal (especially when you already have several others for scripts, images, etc). But why choose between those 2 when there are better options?

share|improve this answer
    
Ok thanks for the answer here, but the plugin I am writing has actually nothing to do with tweets, I took this as an example to understand the problem. I was only actually interested in the comparance of option 1 and 2. I can't use external script, can't use crons. I have just option 1 and 2. –  Saif Bechan Nov 12 '11 at 18:18
1  
@SaifBechan that's okay, most of what I said applies to any external source. If it's not updating constantly then caching for 15 or 30 or 60 minutes is a really good idea. And check on each page load if you need to update the cache. –  DisgruntledGoat Nov 12 '11 at 21:14
    
Yes I already know that, I wrote that also in the question. I know caching is a really efficient way of working, but that was not my question. I think everybody knows that. I was only interested in the difference between option 1 and 2. –  Saif Bechan Nov 13 '11 at 4:42
    
@Saif updated my answer. –  DisgruntledGoat Nov 13 '11 at 11:30
    
I have to admit, in the end I did go for the cache method. Only downside is that I can not use cron, and I can also not fork a new process. Every x times I update the db by a small ajax call from the frontside trough a visitor. Because I could not rely on cron I did want one of option 1 or 2. But well, this has more benefits I guess. Thanks for the help. –  Saif Bechan Nov 18 '11 at 6:10
add comment

since you asked just out of curiosity, I wanted to comment on something . you wrote that loading will take "few MS." well - this is not ALWAYS true. Depending on your target users - sometimes it can take up to 3 min !!! For example, I am right now in CHINA , which , as you know, BLOCKS twitter, facebook, youtube etc. the result : your page will have to wait the TIMEOVER - which sometimes is up to 3 min. - before loading . (and not many users will wait that long - I often bailout) In other countries which I have visited, the "twitterFever" takes up to 20,30sec. load time... So, if your audience is ONLY US, you can discard it . if not - go for the cache .

share|improve this answer
1  
+1 Wow this is good to know, I did not know about this. –  Saif Bechan Nov 14 '11 at 6:34
1  
This would only happen if the server is in China, because it would be the server that's accessing Twitter. –  DisgruntledGoat Nov 16 '11 at 3:01
    
@DisgruntledGoat - that is not true .. firsst - most twitter "plugins" "widgets" etc. use HTTP requests from the BROWSER via JS , JSOn, AJAX etc . this is because the twitter API has a LIMIT of requests for IP. second - even if you use the server to request (less dynamic obviously, how do you update the timeline without reloading the page on timetickers? ) the chinese great firewall will block it . if you do not believe it - go to china and see. or syria, or libia, or north korea. same applies for facebook, youtube, or any other black listed website for those governments. –  krembo99 Nov 17 '11 at 5:34
    
@krembo99 the OP is looking at 2 options, both of which would go through his server rather than directly contacting a third-party server. But even if you're going straight to e.g. Twitter via Javascript, the page will at least load first. –  DisgruntledGoat Nov 17 '11 at 10:32
    
Yes this is true. The data will be gathered from the server, the client has nothing to do with the request. It will however have an impact when the scripts were directly on my website, but that was never an option for me. –  Saif Bechan Nov 18 '11 at 6:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.