Monday, March 26, 2012

instant update from database in aspx page using AJAX

Hi

Can someone point me in the right direction? I have a simple admin screen on my website which shows a tables values from my mssql database. How ever, rather than refreshing every 30 seconds to check for new entries, is there a way using AJAX which instantly updates the admin screen as soon as a new entry is inserted into the database?

thanks

There's not a way for the server to communicate directly with the client, unless the client requests it. The way functionality like what you want is implemented is to continually ping a light weight service that returns information about the last update to the underlying data.

For example, a JSON Web Method that returns the timestamp of the most recent item, which you can compare with the timestamp of the most recent refresh you did. If it's newer than your last update, then you can call a heavier method to update the table.

You can use window.setInterval() in JavaScript to set up the periodic check.


You really have no choice but to set a timer and check the database at a set interval, unless you want to get outside the realm of AJAX and DHTML.

All web based communication is initiated by the client. The only way to have server initiated communication is through an application, such as an AcitveX Object. There are a few (a very few) websites which address this topic but because it is so much more difficult, invasive to the end-users computer, and platform/browser specific, it is rare to find this type of setup.


thanks for your replies, very interesting. I guess the timer option is the way i will have to go. I wish to make the 'service' as light as possible, and the timestamp seems ideal - do you know of any examples, or have any tips on making this as optimal and least processor intensive as possible?

thanks again


You could basically combine these two posts from my blog, with setInterval, to do it:

http://encosia.com/index.php/2007/07/11/why-aspnet-ajax-updatepanels-are-dangerous/

http://encosia.com/index.php/2007/07/13/easily-refresh-an-updatepanel-using-javascript/


thanks gt, great posts - just had a quick browse of the rest of your site, you've got some excellent content on there!!

cheers Yes


I decided to put a simple example together, if anyone else is interested: http://encosia.com/index.php/2007/07/25/display-data-updates-in-real-time-with-ajax/

No comments:

Post a Comment