Ajax Asynchronous refresh. Is it possible ?

P

Piotr Nowak

Hi,

Say i have a server process which listens for some changes in database.
When a change occurs i want to refresh my page in browser by notyfinig it.

I do not want to refresh my page i.e. every 5 seconds, i just want to
refresh it ONLY on server change just like desktop applications do.

The problem is that refreshing evry n seconds has to much impact on my
web server. The refresh action should be taken only when something
really happens that makes sense

Is it possible ?

greets, peter
 
G

Guest

I dont think that is possible, you cant "notify" a web browser client that
something has changed on the server. That is the nature of web sites and web
applications, they are "stateless".
 
P

Piotr Nowak

NH pisze:
I dont think that is possible, you cant "notify" a web browser client that
something has changed on the server. That is the nature of web sites and web
applications, they are "stateless".
stateless means that theres no information about previous request in the
new one, its forgotten.
i just want to get one simple response right from the server when
something happens to update my client browser view.
 
A

Aidy

As long as there is some asp.net code you can run that will return a true or
false as to the server being updated then this is possible. Use an ajax
call every x seconds and when it returns true issue a page reload via
javascript.
 
P

Piotr Nowak

Aidy pisze:
As long as there is some asp.net code you can run that will return a true or
false as to the server being updated then this is possible. Use an ajax
call every x seconds and when it returns true issue a page reload via
javascript.
yes, but the page reload is not as much impact as server side actions
because every x second i would have to do a database hit to check if
something changed, then do a page refresh.
im not shure if i was enough clear..
 
A

Aidy

yes, but the page reload is not as much impact as server side actions
because every x second i would have to do a database hit to check if
something changed, then do a page refresh.
im not shure if i was enough clear..

It's hard to advise on solutions without knowing more about how it can be
determined when a refresh is needed. You say;

"i have a server process which listens for some changes in database.
When a change occurs i want to refresh my page in browser by notyfinig it."

Could your process do something like create/update a file saying to refresh,
your ajax call will see if the data in the file indicates a refresh is
needed. If it is then the call will return that and update the file to set
it back to no refresh being needed. That way every x seconds you are merely
accessing the file system and not the database.
 
P

Patrice

No the server can't reach a web client. This is always the client that sends
a request to the server and the server only responds to incoming requests.

You could issue an AJAX call to the server that just tell if the client
should be updated. If not you have nothing more to do, if yes you can issue
your more complex ajax calls that would update parts of your pages...

Youn may want to explain a bit more what you are trying to do...
 
P

Piotr Nowak

Patrice pisze:
No the server can't reach a web client. This is always the client that sends
a request to the server and the server only responds to incoming requests.

You could issue an AJAX call to the server that just tell if the client
should be updated. If not you have nothing more to do, if yes you can issue
your more complex ajax calls that would update parts of your pages...

Youn may want to explain a bit more what you are trying to do...
Sure its quite generic problem.

Imagine you develop a online game, like ogame.
There's a lot action behind the scenes, players are making some actions
that take some time (training soldiers, building houses) which takes
some time (minutes, hours, even days). after time is up and soldier,
building is finished a browser should be refreshed to say - 'hey, your
request is done now !, you succesfully trained 5 soldiers and built one
house'.

So, it doesnt make sense to poll every 5 seconds if action of building a
house last 2 days ! Divide 2days by 5 seconds and you get number of
waisted requests to check if action is done.

was this enough clear for You ?

best regards
 
P

Patrice

My first idea would be then to avoid polling at a particular fixed interval.
For example the AJAX call could return the next action with an estimated
time to complete (if 0 you can then can then perform the actual action). You
can now use this information to schedule your next AJAX call. If the user
does some action, you'll issue this call again (in case the new action would
be earlier than the previous one) etc...

In some cases the browser is just used to download a software especially if
a quick multiple players interaction is needed...
 
B

bruce barker

the browser can use ajax to poll the server for change. on the server
you should create a in memory collection to store the database change
event. then the ajax poll can be quick.

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top