By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,899 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

Ajax asynchronous is it possible ?

P: n/a
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
Aug 23 '07 #1
Share this Question
Share on Google+
25 Replies


P: n/a
Hi Piotr,

yes, this is possible. Read here how to get more information about it:
http://catb.org/~esr/faqs/smart-questions.html

Best regards,
Jan

Piotr Nowak wrote:
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.
Is it possible ?
--
__________________________________________________ _______________________
insOMnia - We never sleep...
http://www.insOMnia-hq.de
Aug 23 '07 #2

P: n/a
Jan Thomä pisze:
Hi Piotr,

yes, this is possible. Read here how to get more information about it:
http://catb.org/~esr/faqs/smart-questions.html

Best regards,
Jan

Piotr Nowak wrote:
>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.
Is it possible ?
you post a site how to ask questions wisely, it doesnt help me much, sorry.
Aug 23 '07 #3

P: n/a
It will. In time.

Best regards,
Jan

Piotr Nowak wrote:
Jan Thomä pisze:
>Hi Piotr,

yes, this is possible. Read here how to get more information about it:
http://catb.org/~esr/faqs/smart-questions.html

Best regards,
Jan

Piotr Nowak wrote:
>>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. Is it possible ?

you post a site how to ask questions wisely, it doesnt help me much,
sorry.
--
__________________________________________________ _______________________
insOMnia - We never sleep...
http://www.insOMnia-hq.de
Aug 23 '07 #4

P: n/a
Jan Thomä pisze:
It will. In time.

Best regards,
Jan

Piotr Nowak wrote:
>Jan Thomä pisze:
>>Hi Piotr,

yes, this is possible. Read here how to get more information about it:
http://catb.org/~esr/faqs/smart-questions.html

Best regards,
Jan

Piotr Nowak wrote:

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. Is it possible ?
you post a site how to ask questions wisely, it doesnt help me much,
sorry.
if i wasn't searching on google i wouldn't have asked here.
im here because i didnt found solution anywhere else, so i want You,
experts to say what you think about it.
Aug 23 '07 #5

P: n/a
Piotr Nowak wrote:
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
Hi Peter,

This question is more javascript oriented than PHP.
Ajax could be a nice solution. But that only works when visitors have it
enabled of course. I hear different numbers, but around 10% of the
people have it disabled. In that case AJAX won't work of course.

In short:
1) Let the browser make a xmlhttprequest object.
Beware that different browsers need different approach.
Read up here: http://www.w3schools.com/ajax

2) Make a simple php script that your ajax code calls.
Let it return something like: REFRESH or NOREFRESH

3) In case of refresh: refresh. ;-)

Note: Make sure that the PHPscript that returns REFRESH or NOREFRESH is
a simple one. You don't want 10.000 browsers poll your server every 5
seconds...

But one thing: WHY do you want this behaviour? Doesn't everybody know
that a page that is untouched on the browserscreen for 20 hours may be
stale?

Regards,
Erwin Moller
Aug 23 '07 #6

P: n/a
Okay,

before this gets out of hand... My intention is to teach fishing instead of
giving fish. Your question was "Is it possible?" The answer to that
question is "Yes, it is possible.". I have sent you link about asking smart
questions to help you solve your problem. I did not imply that you should
use Google, I'd like you to specify your issue in a way that enables anyone
to help you.

Because currently no one knows, what is your problem. What have you tried so
far? What is the issue you are encountering? Do you need information about
how Ajax is working, or do you need a way to make the database send out
notifications, or what else do you need? What is this data, what triggers
the change? What would you deem as "something interesting is happening"? So
how do you expect to get answers without giving that information? Don't
expect others to ask the right questions for you, ask them yourself. This
is about being helped with a problem not about getting an out of the box
solution. No one will do the homework for you. In that sense, please try
again and take your time to ask a better question and then come back again.

Best regards,
Jan

Piotr Nowak wrote:
if i wasn't searching on google i wouldn't have asked here.
im here because i didnt found solution anywhere else, so i want You,
experts to say what you think about it.
--
__________________________________________________ _______________________
insOMnia - We never sleep...
http://www.insOMnia-hq.de
Aug 23 '07 #7

P: n/a
Piotr Nowak pisze:
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
Let me put it in another way, to help you understand my 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 ?
Aug 23 '07 #8

P: n/a
On 23.08.2007 11:11 Jan Thomä wrote:
Okay,
[snip]
Best regards,
Jan

Piotr Nowak wrote:
>if i wasn't searching on google i wouldn't have asked here.
im here because i didnt found solution anywhere else, so i want You,
experts to say what you think about it.
Please do not top-post.
--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi
Aug 23 '07 #9

P: n/a
On 23.08.2007 09:58 Piotr Nowak wrote:
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 ?
Yes, it's possible in http, however I think you'd be better off with
flash or java applet connected to a non-http socket server.

The correct search term for what you're trying to do is "server push",
hope this helps you further.
--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi
Aug 23 '07 #10

P: n/a
Hi Piotr,
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.
Most probably not. If you have a significant amount of players this will
simply kill your database. One approach could be to hold a queue of events
for every player in the database, together with a time when they will
trigger. Once the player logs in, this queue is loaded once by a
javascript, which will then send out the notifications based on the loaded
data without the need of queriying the database again. You just need to
make sure that the list on the client is refreshed when there are changes
to the list (e.g. the player cancels something or adds a new item into the
queue). Since these actions are triggered by the player it should not be
too difficult to do that. The only tricky part is, when you get
notifications upon actions of other players as then you would need to poll
the database. However unless the notifications should be sent in real time,
polling can be reduced to an interval of some minutes or so. Another
approach would be to add the notifications to the content that is sent
anyways to the browser when the player changes to another page...

Best regards,
Jan
--
__________________________________________________ _______________________
insOMnia - We never sleep...
http://www.insOMnia-hq.de
Aug 23 '07 #11

P: n/a
Jan Thomä pisze:
The only tricky part is, when you get
notifications upon actions of other players as then you would need to poll
the database. However unless the notifications should be sent in real time,
polling can be reduced to an interval of some minutes or so.
Best regards,
Jan


Thats the tricky part im talking about. You want to see everything in
real time. you would have to do a browser refresh everytime somebody
attacked you instead of notifying it immediatly.

There's a server-push technique, but it involves flash or java applet
http://www.bluishcoder.co.nz/2005/11...rver-push.html

interesting isn't it ?
Aug 23 '07 #12

P: n/a
There's a server-push technique, but it involves flash or java applet
http://www.bluishcoder.co.nz/2005/11...rver-push.html
I am not quite sure, if this is the way to do it. Given you have 10k
players, you would have 10k persistent connections on the server. IMHO this
server push isn't a real push anyways, its just a glorified pull. Question
is, how does an attack work anyways? Does the user need to immediately
reply to an attack? What is if the user is offline when he is attacked?
Maybe one could set like a 24 hour response time to an attack, which would
eliminate the need for an immediate notification. A check every 20 minutes
or so would be enough then and would put a lot less stress on the server.
The other possibilitiy is to send the data everytime the user loads a new
page (which is something he probably has to do anyways when playing the
game). This would keep the notification overhead low and still allow for a
somewhat timely notification at least if the user does something other than
wait before his screen... How about that?

Best regards,
Jan
--
__________________________________________________ _______________________
insOMnia - We never sleep...
http://www.insOMnia-hq.de
Aug 23 '07 #13

P: n/a
Piotr Nowak wrote:
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
Despite some other comments, no, it's not really possible with HTTP
protocol. HTTP is a "request/response" protocol - the browser sends a
request and the server responds. There really isn't a mechanism for
sending unsolicited responses.

As others have mentioned, it is possible to to with other means, i.e.
Java or Flash. But not in PHP.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 23 '07 #14

P: n/a
Jan Thomä wrote:
Hi Piotr,

yes, this is possible. Read here how to get more information about it:
http://catb.org/~esr/faqs/smart-questions.html

Best regards,
Jan

Piotr Nowak wrote:
>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.
Is it possible ?
You should take some of your own advice. The question was very clear,
but your response was lousy.

And please don't top post.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 23 '07 #15

P: n/a
Jan Thomä pisze:
>There's a server-push technique, but it involves flash or java applet
http://www.bluishcoder.co.nz/2005/11...rver-push.html

I am not quite sure, if this is the way to do it. Given you have 10k
players, you would have 10k persistent connections on the server. IMHO this
server push isn't a real push anyways, its just a glorified pull. Question
is, how does an attack work anyways? Does the user need to immediately
reply to an attack? What is if the user is offline when he is attacked?
Maybe one could set like a 24 hour response time to an attack, which would
eliminate the need for an immediate notification. A check every 20 minutes
or so would be enough then and would put a lot less stress on the server.
The other possibilitiy is to send the data everytime the user loads a new
page (which is something he probably has to do anyways when playing the
game). This would keep the notification overhead low and still allow for a
somewhat timely notification at least if the user does something other than
wait before his screen... How about that?

Best regards,
Jan

Seems to be quite interesting idea.
will see on simple app how does it work !

THANK YOU !
Aug 23 '07 #16

P: n/a
Jerry Stuckle wrote:
Piotr Nowak wrote:
>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

Despite some other comments, no, it's not really possible with HTTP
protocol. HTTP is a "request/response" protocol - the browser sends a
request and the server responds. There really isn't a mechanism for
sending unsolicited responses.

As others have mentioned, it is possible to to with other means, i.e.
Java or Flash. But not in PHP.
Well, it IS possible with javascript and polling.
It stinks, depends on javascript, creates serverload, etc, but it IS
possible.
But I agree it has nothing to do with PHP. :-)

Regards,
Erwin Moller
Aug 23 '07 #17

P: n/a
Jerry Stuckle wrote:
You should take some of your own advice. The question was very clear,
but your response was lousy.
It was not. To avoid repeating myself: Check article
<5j*************@mid.individual.net>.
And please don't top post.
Thanks again for the reminder.

Best regards,
Jan

--
__________________________________________________ _______________________
insOMnia - We never sleep...
http://www.insOMnia-hq.de
Aug 23 '07 #18

P: n/a
Jan Thomä wrote:
Jerry Stuckle wrote:
>You should take some of your own advice. The question was very clear,
but your response was lousy.
It was not. To avoid repeating myself: Check article
<5j*************@mid.individual.net>.
>And please don't top post.
Thanks again for the reminder.

Best regards,
Jan
Then why did I (and it seems several other people) seem to understand
what he was trying to do just fine?

I don't want to get into a pissing match over this - but it was clear
enough to several of us. And a lot better than some of the messages
I've seen posted here.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 23 '07 #19

P: n/a
Erwin Moller wrote:
Jerry Stuckle wrote:
>Piotr Nowak wrote:
>>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

Despite some other comments, no, it's not really possible with HTTP
protocol. HTTP is a "request/response" protocol - the browser sends a
request and the server responds. There really isn't a mechanism for
sending unsolicited responses.

As others have mentioned, it is possible to to with other means, i.e.
Java or Flash. But not in PHP.

Well, it IS possible with javascript and polling.
It stinks, depends on javascript, creates serverload, etc, but it IS
possible.
But I agree it has nothing to do with PHP. :-)

Regards,
Erwin Moller
Erwin,

He said he wants to notify the browser, and he wants to do it without
refreshing every few seconds. I take that to also mean polling.

Even 1K players polling every 5 seconds would place a heck of a load on
the server.

HTTP just isn't set up for this sort of thing.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 23 '07 #20

P: n/a
Jerry Stuckle wrote:
Erwin Moller wrote:
>Jerry Stuckle wrote:
>>Piotr Nowak wrote:
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

Despite some other comments, no, it's not really possible with HTTP
protocol. HTTP is a "request/response" protocol - the browser sends
a request and the server responds. There really isn't a mechanism
for sending unsolicited responses.

As others have mentioned, it is possible to to with other means, i.e.
Java or Flash. But not in PHP.

Well, it IS possible with javascript and polling.
It stinks, depends on javascript, creates serverload, etc, but it IS
possible.
But I agree it has nothing to do with PHP. :-)

Regards,
Erwin Moller

Erwin,

He said he wants to notify the browser, and he wants to do it without
refreshing every few seconds. I take that to also mean polling.
Hi Jerry,

You are correct. I misinterpreted this line:
"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."

That gave me the impression he wanted to poll using AJAX, instead of
refreshing the whole page. And then refresh the whole page if the
servers says ie is time to refresh.

>
Even 1K players polling every 5 seconds would place a heck of a load on
the server.
Yes. Serious drawback of polling like that.
>
HTTP just isn't set up for this sort of thing.
True, but it can be misused for it.
Long ago I made a site do just that (before Ajax/xmlhttprequests) via
hidden frame refresh.
It sucked of course. ;-)

Java <--JavaScript is a much cleaner solution.
But if memory serves me well the visitor had to give the applet rights
to listen to another port than 80.
I am kind of rusty with Java these days, so things might have changed.

Regards,
Erwin Moller
Aug 23 '07 #21

P: n/a
He said he wants to notify the browser, and he wants to do it without
refreshing every few seconds. I take that to also mean polling.
That gave me the impression he wanted to poll using AJAX, instead of
refreshing the whole page. And then refresh the whole page if the
servers says ie is time to refresh.
There is a JavaDev project that works on this kind of stuff. Not sure
how much it may help you, but it's intent is just this.

https://glassfish.dev.java.net/
HTTP just isn't set up for this sort of thing.
Correct.
Good luck,
Thiago

Aug 23 '07 #22

P: n/a
Erwin Moller wrote:
Jerry Stuckle wrote:
>>
HTTP just isn't set up for this sort of thing.

True, but it can be misused for it.
Long ago I made a site do just that (before Ajax/xmlhttprequests) via
hidden frame refresh.
It sucked of course. ;-)
And how often have we seen things misused by incompetent "programmers"? :-)
Java <--JavaScript is a much cleaner solution.
But if memory serves me well the visitor had to give the applet rights
to listen to another port than 80.
I am kind of rusty with Java these days, so things might have changed.

Regards,
Erwin Moller
It depends on your security setup - but if you do need to give it
rights, it's easy enough to do so.

Of course, there is another downside to this - maintaining what could be
thousands of connections at the server. That has its own overhead.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 23 '07 #23

P: n/a
Jerry,

Jerry Stuckle wrote:
I don't want to get into a pissing match over this
Agreed.

Let's keep it on that. As you might have seen Piotr and me had a nice
discussion afterwards, which gained some results for Piotr as well, so no
one needs to be pissed. Case closed from my side.

Best regards,
Jan

--
__________________________________________________ _______________________
insOMnia - We never sleep...
http://www.insOMnia-hq.de
Aug 23 '07 #24

P: n/a
Jerry Stuckle pisze:
Erwin Moller wrote:
>Jerry Stuckle wrote:
>>>
HTTP just isn't set up for this sort of thing.

True, but it can be misused for it.
Long ago I made a site do just that (before Ajax/xmlhttprequests) via
hidden frame refresh.
It sucked of course. ;-)

And how often have we seen things misused by incompetent "programmers"? :-)
>Java <--JavaScript is a much cleaner solution.
But if memory serves me well the visitor had to give the applet rights
to listen to another port than 80.
I am kind of rusty with Java these days, so things might have changed.

Regards,
Erwin Moller

It depends on your security setup - but if you do need to give it
rights, it's easy enough to do so.

Of course, there is another downside to this - maintaining what could be
thousands of connections at the server. That has its own overhead.
Two drawbacks:

- firewall on client machines
- too many connections

thanks for advice anyway :)
Aug 24 '07 #25

P: n/a
Piotr Nowak wrote:
Jerry Stuckle pisze:
>Erwin Moller wrote:
>>Jerry Stuckle wrote:
HTTP just isn't set up for this sort of thing.
True, but it can be misused for it.
Long ago I made a site do just that (before Ajax/xmlhttprequests) via
hidden frame refresh.
It sucked of course. ;-)

And how often have we seen things misused by incompetent
"programmers"? :-)
>>Java <--JavaScript is a much cleaner solution.
But if memory serves me well the visitor had to give the applet
rights to listen to another port than 80.
I am kind of rusty with Java these days, so things might have changed.

Regards,
Erwin Moller

It depends on your security setup - but if you do need to give it
rights, it's easy enough to do so.

Of course, there is another downside to this - maintaining what could
be thousands of connections at the server. That has its own overhead.

Two drawbacks:

- firewall on client machines
- too many connections

thanks for advice anyway :)
The firewall would have to be adjusted, of course.

And while I normally don't recommend maintaining open connections like
this, the overhead of opening and closing thousands of connections every
10 seconds would be much higher on both the server and the network.

Establishing connections is an expensive process, both in cpu cycles and
network. Maintaining open connections takes very little of the network
resources, a little server memory and a few CPU cycles.

Yes, you're wasting server resources by keeping rarely used connections
open, but you're wasting even more by constantly opening and closing them.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 24 '07 #26

This discussion thread is closed

Replies have been disabled for this discussion.