views:

216

answers:

9

I'm trying to figure out if there's a way I can somehow notify a web browser of an event from a web server.

I know Ajax lets you asynchronously make a request to update a section of a page. I guess I could have a timer periodically ask for updates. But, I'd prefer to avoid a polling scheme, if possible. Is there a better way to go and still remain with a browser-based solution?

A: 

You can use partial rendering. I would check out this article for more information.

Here is another article on the topic.

Kyle Trauberman
I don't see what relevance this has to the original question.
Adam Lassek
+2  A: 

Well, you could try to set the "ignore-user-abort"-flag on, and make sure that the script doesn't terminates (while condition) sleep()). After you have echo'ed the information you need to transfer, flush() the text to the browser.

But i would't recommend this solution. Instead: Go with ajax, and use a polling scheme. Most up-to-date framework support it out of the box.

qualbeen
A: 

Hey Jack,

I have a good link for you http://java.dzone.com/articles/asynchronous-ajax-revolutionar

hope this helps.

gnosio
+3  A: 

Check out "comet" techniques, where you basically hold a connection open to a server which pushes data back at you.

Paul Dixon
+1  A: 

I guess I could have a timer periodically ask for updates. But, I'd prefer to avoid a polling scheme, if possible.

Tough luck: that's what you gotta do. The web is built on a request/response model: 1 request from the browser, 1 response from the server, and always in that order.

That said, you don't have to (and probably shouldn't) build that polling scheme yourself. You can probably find an existing implementation that abstracts away the details and make it look like the server is notifying the client.

Joel Coehoorn
A: 

There is Server-Sent Events from the WHATWG Web Applications 1.0 specification that was added to Opera 9. Mozilla/Firefox seem to be working on it.

Zitrax
+2  A: 

Comet is the thing you are looking for. There are some js libraries and http servers that make it easier to use. It's based on the idea of keeping connections open with a certain request and streaming back to the browser when the server has something to stream. You should be aware of he fact that browsers usually can have a very limited number of connections open to one domain (typicaly one I think). If you like to try this out take a look at:

dojo cometd

js io

orbited

apache tomcat advanced io

If you are into erlang check this:

http://yoan.dosimple.ch/blog/2008/05/15/

Vasil
A: 

In the past I found nice article about streaming data in HTML:

http://ajaxpatterns.org/HTTP_Streaming

Can be usefull :)

Thinker
A: 

Also check out juggernaut for RubyOnRails here

Will