views:

134

answers:

7

Hi

What method would you choose for retrieving tweets from Twitter, for example?

  • Using json with jQuery (like http://juitter.com/), where client does the request.
  • using PHP, for eg. CURL, where server does the request

What are the advantages/disadvantages of both?

A: 
//for client making request   

     jQuery.ajax({
                url:'target_url',
                type:'get',
                dataType:'jsonp',
                success:function(data){
                      //do something with data
                   } 
        });

    //note: dataType is jsonp not JSON as calling twitter from your domain would 
    //not be allowed by browser(cross domain request are not allowed) 
    //so u have  use jsonp.
Praveen Prasad
+2  A: 

Using server side approach, you may benefit from caching facilities, and having an overall faster page load (no roundtrips to twitter, or any other 3rd party service on each request).

The other benefit of the server side approach is, users coming from countries (or even companies) which twitter is filtered in, won't see a big empty pane, or even worse a mangled layout. And even when twitter goes bad, you have a cache somewhere to show the recent tweets.

But the most obvious advantage of client side approach is being hassle free.

racetrack
+1  A: 

I think it depends on how frequently the stream you are pulling in gets updated. If it isvery frequently then JS is better, because you can continually run the call without refreshing the page, whereas not so frequently, and you can pull all the data in using CURL and use JS for animations.

Liam Bailey
+1  A: 

Client-side requests are better when you have to do many requests (e.g. for use by a public site) as you lower your server load / avoid a bottleneck, possibly benefitting from content-delivery networks (CDN) caching the requests on behalf of your web clients and you move some of the liability from yourself to the users of your site as they are actually accessing the third-party API (which may be more relevant if you have really many requests; some API terms of use even limit the number of requests per time unit so that client-side requests are the only option for big sites).

Server-side requests have the advantage of not requiring JavaScript to be enabled on the client side and can also be logged easily for statistical purposes or processed further.

Archimedix
+1  A: 

There is a distinction between the both approaches. When using server side outside HTTP requests, you retrieve the data to the server. If you want that data to be seen to client, you have to send it with the next request the client does to your server side. With server side requests you can do native HTTP requests and cross-domain requests also.

Client side cross-domain requests retrieve the data directly to the client. You can display that to the client in the same instance when the request returns data but if you want the data on the server side (storing the tweets in db) you have to send them from the client, back to the server. Javascript can't do cross-domain XHR requests. In order to do that, you (or the libs that do that) make some workarounds: Using iframes, using including of JS files that already have the info that you need etc.

If you need to consume a web service, I advice on using the backend as the service client and either use timely pull from the client side, or use some of the "comet" techniques.

dekomote
A little side-note: Cross-domain requests can also be made using JSON with padding (JSONP) or Cross-Origin Resource Sharing (CORS). http://en.wikipedia.org/wiki/JSON#JSONP, http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing
Archimedix
+1  A: 

using server as "MITM" you may cache, alter, insert data from 3rd part before published to your users. Your users may dislike it, though...

+1  A: 

I would prefer client side for displaying tweets rather than the server side because of the following reasons:-

  • Since tweets are displayed directly on the browser in real time, using jQuery complements well with this scenario. This refreshes the page quickly along with your tweets (using multiple asynchronous ajax calls ofcourse).
  • Well when you take the server side, it takes an extra roundtrip to the server and back to the client. Do you need that trip when all you need is to display just tweets??. Tweets are just social messages (say hello!!!) stuff and they are not supposed to contain any harmful stuff.
  • You generally take the server side approach when you need to do data validation before displaying on the browser. For tweets, it's not necessary.
A_Var