Connecting Tech Pros Worldwide Help | Site Map

refresh page on database change

Ciary's Avatar
Expert
 
Join Date: Apr 2009
Location: The outer ring of hell
Posts: 238
#1: May 27 '09
hey all,

i'm going to post my next question here since i have no idea where to start. my problem is this:
i'm trying to this. i have a page from where i read all records from a table. when the admin adds a new record to a database, all visitors should get a refresh request or they should automatically refresh.

a more practical example (to better explain what i mean) a radiostation keeps a playlist (starting from their last played song) on their index page. when the radiostation starts a new song, it is added to the database. then, all users visiting the main page at that moment should refresh their browser.

what i do now is read the time of the song and fill it in into a header("refresh") but this can be done a lot better i think.

does anyone have any idea how this can be done?
Member
 
Join Date: Feb 2009
Location: Jersey
Posts: 35
#2: May 27 '09

re: refresh page on database change


An example based on C#

You should try looking at the ASP.NET Life cycle and understand - one of the page events should call the method which you used to load the database in the first place.

This site is good:
http://www.15seconds.com/issue/020102.htm

If I was doing this, I would go for the OnPreRender() method and put the code you want to be called ONCE in an IsPostBack clause, then all the code you want to be called each time, including your method to load the database outside the clause, like so:

Expand|Select|Wrap|Line Numbers
  1. protected override void OnPreRender(EventArgs e)
  2.     {
  3.         // code run each time the page is rendered
  4.         // e.g. - the method to load your database
  5.  
  6.         if (!IsPostBack)
  7.         {
  8.             // code to be run only once
  9.         }
  10.  
  11.         base.OnPreRender(e);
  12.     }
  13.  
Ciary's Avatar
Expert
 
Join Date: Apr 2009
Location: The outer ring of hell
Posts: 238
#3: May 27 '09

re: refresh page on database change


so if i understand correctly i need all things executed once in the postback clause so i dont execute things twice (like insert queries for example) the query in which i read the database will be outside this clause.

so far so good, but how do i trigger events like onDatabaseChange.or how do i add a changelistener to it??

already tnx, ive never used ASP.NET before so i have no idea how to code anything in it.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#4: May 27 '09

re: refresh page on database change


Quote:

Originally Posted by Ciary View Post


so far so good, but how do i trigger events like onDatabaseChange.or how do i add a changelistener to it??

already tnx, ive never used ASP.NET before so i have no idea how to code anything in it.

Web applications (like an ASP.NET web application) are stateless apps.

This is rather limiting....let's examine this a bit:

ASP.NET applications exist on a Web Server. They are compiled (unless they are already precompiled) and executed when a browser makes a request that involves them.

The ASP.NET application does stuff (like retrieves things from databases etc) and then sends a the resulting response (a web page) to the browser...which displays the web page to the end user.

Now the admin makes a change database...and your data access layer raises an onDatabaseChange event....

Even if your ASP.NET application was listening for the event, what is it supposed to do with it?

The only way that the ASP.NET application can send information to the browser is if a request was made to it in the first place... Otherwise how would it know where (what browser, what IP) to send it to? How would it even send the response if the web browser isn't waiting for a response from the server?

Since it can't know these things, it can't do anything with the event...it can't update any web browsers currently displaying the web page because it simply cannot do this without a request from the web browser for the page.


My point here is if you are developing a web application (you never stated the type of app you're developing) it doesn't matter if the database has changed until a web browser makes a request to the web application.

The onDatabaseChange event is not needed if you implement the web application so that it accesses a live database (which contains the updated information) on every request is made by the browser.

This would mean that you would not cache the data retrieved from the database first time the page is loaded.

There is a neat way to cache your data...in ASP.NET though.

See:
It's a lot easier if you just simply use a live database and refresh your data on every web request.


In order to achieve what you are attempting (updating all browsers displaying the web page when the database is updated), you'd have to constantly ask the server for an updated list. You'll have to have some sort of client side (JavaScirpt/Ajax) timer that pings the server constantly since the web server cannot send a response to a browser without the browser asking for it. This is probably not a good idea because it's going to waste bandwidth and server resources.
Member
 
Join Date: Feb 2009
Location: Jersey
Posts: 35
#5: May 27 '09

re: refresh page on database change


If you have placed a request to lead the database in your OnPreRender() method, then when you or someone using your site clicks a button to confirm a new row, the OnPreRender() will execute the same functionality as you have told it to do at the beginning (i.e. - Load the database records using an SQL query).

Frinavale's post was a bit complicated but he's right - you have to right code to change the state of controls - adding a row into your SQL table by hand won't automatically refresh the page.

Good luck!
Matt
Ciary's Avatar
Expert
 
Join Date: Apr 2009
Location: The outer ring of hell
Posts: 238
#6: May 28 '09

re: refresh page on database change


so basically, since you're saying it isnt possible, what i'm doing now is the best option:
which means i use header(refresh) in php and calculate the time depending on the songs length(retreived from the database)?
-> leads to asynchronous refreshes

- or -

i can fill a database with refreshtimes. php will then calculate the time 'till the next refresh and use it in header(refresh)
-> will synchronize all refreshes but might slow the server on a refresh

maybe i should use the last method and then add a random generated number from 0 to 15 seconds to the refresh-time so there is a small spread in which there is refreshed
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#7: May 30 '09

re: refresh page on database change


How annoying would that be? You're doing something on a webpage, reading an article, whatever, and then, out-of-the-blue, the page refreshes.

Updating the whole page would be overkill. Use javascript to update just a portion of it; that way a visitor isn't broken away from whatever they're doing.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#8: Jun 1 '09

re: refresh page on database change


Quote:

Originally Posted by Markus View Post

How annoying would that be? You're doing something on a webpage, reading an article, whatever, and then, out-of-the-blue, the page refreshes.

This happened to me when I was playing a game of majon...when it posted back it destroyed my game even though I was doing really well that round! So annoying!

Quote:

Originally Posted by Markus View Post

Updating the whole page would be overkill. Use javascript to update just a portion of it; that way a visitor isn't broken away from whatever they're doing.

Markus is right, consider using Ajax to update only the portion of the page that requires refreshing.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#9: Jun 1 '09

re: refresh page on database change


Instead of poll/pull, you'll want to look at push technologies. See Comet - also known as Reverse Ajax, Http Streaming, Ajax Push, Server Push, Slow Load Technique and so on.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#10: Jun 1 '09

re: refresh page on database change


Quote:

Originally Posted by acoder View Post

Instead of poll/pull, you'll want to look at push technologies. See Comet - also known as Reverse Ajax, Http Streaming, Ajax Push, Server Push, Slow Load Technique and so on.

Interesting!
I think I'm going to have to try the long polling technique later today.

Thanks a lot acoder.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#11: Jun 30 '09

re: refresh page on database change


After investigating the issues that occur with keeping an HTTP connection open for a long time, I found that there are other ways to accomplish this. Most solutions recommend that you use a Flash Object but I've discovered that Silverlight now supports Sockets.

I'm enjoying this article on how to use Silverlight to push notifications with Socket Server. I hope to implement a solution using this technique.

Thanks again acoder!
I would never have known this was possible if you hadn't mentioned Comet :)
Reply