views:

815

answers:

5

hi there

I need to keep certain data ( in a grid) up to date and was gonna do a poll to the server every 15 seocnds or so to get the data and refresh the grid, however it feels a bit dirty ( the grid will have the loading icon every 15 sec..) doesnt look great...
Another option is to check if there is new data and compare the new data with the current data and only refresh the grid if there is any changes ( I would have to do this client side tho because maintaing the current state of every logged in user also seems like an overkill)
I m sure there are better solutions and would love to hear about them

I heard about COMET, but tit seems to be a bit of an overkill
BTW i m using asp.net MVC on the server side

I d like to hear what people have to say for or against continuos polling with js Cheers

+1  A: 

Every time you get the answer from the server, check if something has changed.

Do a request. Do let the user know that you are working with some spinner, don't hide it. Schedule the next request in 15 seconds. The next request executes; if nothing has changed, schedule the next one in 15 + 5 seconds. The next request executes; if nothing has changed, schedule the next on in 15 +5 +5 seconds. And so on. The next request executes; if something has indeed changed, reset the interval to 15 seconds.

Prototype can do this semi-automatically with Ajax.PeriodicalUpdater but you probably need stuff that is more customized to your needs.

Anyway, just an idea.


As for continuous polling in general; it's bad only if you hit a different site (using a PHP "bridge" or something like that). If you're using your own resources you just have to make sure you don't deplete them. Set decent intervals with a decay.

Kaze no Koe
Thanks for the answerFor the type of application I m trying to build, updaets need to be constant, they cant decay
Miau
A: 

I think checking if there is any new data is a good option.

I would count the number of rows in the database and compare that with the number of rows in your (HTML) table. If they're not the same, get the difference in rows.

Say you got 12 table rows and there are 14 database rows as you check: Get the latest (14 - 12) = 2 rows.

richard
+1  A: 

I suggest Comet is not an overkill if "updates need to be constant." 15 seconds is very frequent; is your visited by many? Your server may be consumed serving these requests while starving others.

Upper Stage
Is in this kinds of situations that I think, damn there must be a good load testing tool around..
Miau
How about http://seleniumhq.org/ or http://www.badboy.com.au/I've used BadBoy to load test (some time ago) and I liked it.
Upper Stage
I ve tried selenium but it really doesnt seem to scale well , maybe I m not doing it right? Ie I can test automating a few browsers simultaneously but thats it.
Miau
I'm afraid I don't have a great deal of hands-on experience with Selenium, though I can write I worked on a project (25 developers) where we dedicated 4 developers full-time to Selenium and automated tests. We had the full backing of our government customer. The demonstrations/tests were impressive.
Upper Stage
+2  A: 

Sounds like COMET is indeed the solution you're looking for. In that scenario, you don't need to poll, nor do comparisons, as you can push out only the "relevant" changed data to your grid.

Check out WebSync, it's a nice comet server for .NET that'll let you do exactly what you've described.

Here's a demo using ExtJS and ASP.NET that pushes a continuous stream of stock ticker updates. The demo is a little more than you need, but the principal is identical.

jvenema
+1  A: 

I don't know what your server-side data source looks like, or what kind of data you're serving, but one solution is to server your data with a timestamp, and send a timestamp of the last poll with every subsequent request.

  1. Poll the server, sending the timestamp of when the service was last polled (eg: lastPollTime).
  2. The server uses the timestamp to determine what data is new/updated and returns only that data (the delta), decreasing your transmission size and simplifying your client-side code.
  3. It may be empty, it may be a few cells, it may be the entire grid, but the client always updates data that is returned to it because it is already known to be new.

The benefits of this method are that it simplifies your client side code (which is less code for the client to load), and decreases your transmission size for subsequent polls that have no new data for the user.

Also, this allows you to maintain state on the server side because you don't have to save a state for each individual user. You just have one state, the state of the current data, that is differentiated by access time.

Justin Johnson