views:

498

answers:

10

It's a simple case of a javascript that continuously asks "are there yet?" Like a four year old on a car drive.. But, much like parents, if you do this too often or, with too many kids at once, the server will buckle under pressure..

How do you solve the issue of having a webpage that looks for new content in the order of every 5 seconds and that allows for a larger number of visitors?

+4  A: 

stackoverflow does it some way, don't know how though.

The more standard way would indeed be the javascript that looks for new content every few seconds.

A more advanced way would use a push-like technique, by using Comet techniques (long-polling and such). There's a lot of interesting stuff under that link.

I'm still waiting for a good opportunity to use it myself...

Oh, and here's a link from stackoverflow about it:
http://stackoverflow.com/questions/19995/is-there-some-way-to-push-data-from-web-server-to-browser

Berzemus
Oh, I forgot to answer the main question: by using long-polling techniques, server load should decrease, as a connection may lay idle for much longer, eventually re-opening itself if it happens to die.
Berzemus
+1  A: 

You could take a look at the 'Twisted' framework in python. It's event-driven network programming framework that might satisfy what you are looking for. It can be used to push messages from the server.

trex279
+1  A: 

Perhaps you can send a query to a real simple script, that doesn't need to make a real db-query, but only uses a simple timestamp to tell if there is anything new.

And then, if the answer is true, you can do a real query, where the server has to do real work !-)

roenving
A: 

Check out this link, it explains the various web remoting techniques

Pablo Cabrera
+2  A: 

In Java I used Ajax library (DWR) using Comet technology - I think you should search for library in PHP using it. The idea is that server is sending one very long Http response and when it has something to send to the client it ends it and send new response with updated data. Using it client doens't have to ping server every x seconds to get new data - I think it could help you.

pbrodka
+2  A: 

You could make the poll time variable depending on the number of clients. Using your metaphor, the kid asks "Are we there yet?" and the driver responds "No, but maybe in an hour". Thankfully, Javascript isn't a stubborn kid so you can be sure he won't bug you until then.

monzee
LOL, Awesome analogy!
Jeff Sheldon
+2  A: 

You could consider polling every 5 seconds to start with, but after a while start to increase the poll interval time - perhaps up to some upper limit (1 minute, 5 minute - whatever seems optimal for your usage). The increase doesn't have to be linear.

A more sophisticated spin (which could incorporate monzee's suggestion to vary by number of clients), would be to allow the server to dictate the interval before next poll. The server could then increase the intervale over time, and you can even change the algorithm on the fly, or in response to network load.

Phil Nash
A: 

I would have a single instance calling the DB and if a newer timestamp exists, put that new timestamp in a application variable. Then let all sessions check against that application variable. Or something like that. That way only one innstance are calling the sql-server and the number of clients does'nt matter.

I havent tried this and its just the first idéa on the top of the head but I think that cashe the timestamp and let the clients check the cashe is a way to do it, and how to implement the cashe (sql-server-cashe, application variable and so on) I dont know whats best.

Stefan
Please tell me why my idea is bad so I can learn from it. //Thanks.
Stefan
A: 

Regarding how SO does it, note that it doesn't check for new answers continuously, only when you're typing into the "Your Answer" box.

The key then, is to first do a computationally cheap operation to weed out common "no update needed" cases (e.g., entering a new answer or checking a timestamp) before initiating a more expensive process to actually retrieve any changes.

Alternately, depending on your application, you may be able to resolve this by optimizing your change-publishing mechanism. For example, perhaps it might be feasible for changes (or summaries of them) to be put onto an RSS feed and have clients watch the feed instead of the real application. We can assume that this would be fairly efficient, as it's exactly the sort of thing RSS is designed and optimized for, plus it would have the additional benefit of making your application much more interoperable with the rest of the world at little or no cost to you.

Dave Sherohman
A: 

I believe the approach shd be based on a combination of server-side sockets and client-side ajax/comet. Like:

Assume a chat application with several logged on users, and that each of them is listening via a slow-load AJAX call to the server-side listener script.

Whatever browser gets the just-entered data submits it to the server with an ajax call to a writer script. That server updates the database (or storage system) and posts a sockets write to noted listener script. The latter then gets the fresh data and posts it back to the client browser.

Now I haven't yet written this, and right now I dunno whether/how the browser limit of two concurrent connections screws up the above logic.

Will appreciate hearing fm anyone with thoughts here.

AS