Howdy_McGee
Howdy_McGee

Reputation: 10673

Multiple AJAX Requests - Troublesome?

Right now I'm using AJAX to pull in a list of active streams (TwitchTV) and it's viewers and I'm requesting this every second. At time the list of streams to check can get quite lengthy so I plan on splitting the ajax requests into 2 or 3 parts:

1) Get Number of Viewers for Current Stream (Check every 1 Second)

2) Split Stream in Half and Check 1st Half of List for Active Streamers (Check every 5 Seconds)

3) Check 2nd Half of List for Active Streamers (Check every 5 Seconds)

so I would have 3 requests running simultaneously but I'm worried about what the load time will come down to. Since it is constantly pulling in data would it make the page slower? Would the user likely notice? Is it better to keep 1 ajax request for big amounts of data or is it better to use multiple ajax requests for smaller pieces of data? Is ajax really the best thing to pull in constantly changing live data?

Upvotes: 0

Views: 247

Answers (1)

Wade Tandy
Wade Tandy

Reputation: 4144

The answer to your various questions is probably "It depends":

  1. The ajax requests by themselves shouldn't make anything slower. These are asynchronous requests, so they will only actually cause the user's browser any significant (and probably still not noticeable) load when the request completes.

  2. One thing that could potentially slow your app down (or cause the user to notice in an unpleasant way) is the DOM manipulation when the request completes. Changing your current number of streaming users in-place probably won't hurt, but depending on the number of streams/how you are displaying them in a list, redrawing this could potentially be very expensive/cause lag on things like redraw.

  3. An alternative to using Ajax (depending on what browsers you wish to support) is to use websockets. This way you can keep a connection open and the server can tell the application when the data needs to change, instead of the need to poll for it.

  4. Why do you need to break your list up into a first half and a second half?

  5. One way to cut down on the amount of data you're sending back and forth might be to send some sort of signal indicating the last bit of data you received. For example, when your timeline on twitter.com updates every few seconds, the ajax request sends along the id of the most recent tweet it received, so that the server knows not to waste time sending any data older than that. Depending on your use case this might be effective.

Upvotes: 1

Related Questions