views:

3877

answers:

18

Of course I am aware of Ajax, but the problem with Ajax is that the browser should poll the server frequently to find whether there is new data. This increases server load.

Is there any better method (even using Ajax) other than polling the server frequently?

+22  A: 

Yes, what you're looking for is COMET http://en.wikipedia.org/wiki/Comet_(programming). Other good Google terms to search for are AJAX-push and reverse-ajax.

Greg Hurlman
+5  A: 

Look into Comet (a spoof on the fact that Ajax is a cleaning agent and so is Comet) which is basically "reverse Ajax." That's exactly what you're looking for. Be aware that this can be a little server intensive from what I've heard, but it's exactly what you're looking for i think.

http://en.wikipedia.org/wiki/Comet_(programming)

Dan Herbert
A: 

I am not sure there is. To make it conceptually simpler for the application I guess you could implement a transport layer on top of the polling requests, and thus removing the polling responsibility from your application logic. Maybe someone even already implemented this?

Edit: Apparently it's called reverse Ajax and Comet, but so far it looks like you have to implement it yourself. A JavaScript library for this, anyone?

Anders Sandvig
+1  A: 

Once a connection is opened to the server it can be kept open and the server can Push content a long while ago I did with using multipart/x-mixed-replace but this didn't work in IE.

I think you can do clever stuff with polling that makes it work more like push by not sending content unchanged headers but leaving the connection open but I've never done this.

sparkes
+1  A: 

There are other methods. Not sure if they are "better" in your situation. You could have a Java applet that connects to the server on page load and waits for stuff to be sent by the server. It would be a quite a bit slower on start-up, but would allow the browser to receive data from the server on an infrequent basis, without polling.

Kibbee
A: 

You can also look into Java Pushlets if you are using jsp pages.

Ryan Ahearn
+1  A: 

It's possible to achive what you're aiming at through the use of persistent http connections.

Check out the Comet article over at wikipedia, that's a good place to start.

You're not providing much info but if you're looking at building some kind of event-driven site (a'la digg spy) or something along the lines of that you'll probably be looking at implementing a hidden IFRAME that connects to a url where the connection never closes and then you'll push script-tags from the server to the client in order to perform the updates.

Markus Olsson
+1  A: 

I would strongly suggest to invest some time on Comet, but I dont know an actual implementation or library you could use.

For an sort of "callcenter control panel" of a web app that involved updating agent and call-queue status for a live Callcenter we developed an in-house solution that works, but is far away from a library you could use.

What we did was to implement a small service on the server that talks to the phone-system, waits for new events and maintains a photograph of the situation. This service provides a small webserver.

Our web-clients connects over HTTP to this webserver and ask for the last photo (coded in XML), displays it and then goes again, asking for the new photo. The webserver at this point can:

  • Return the new photo, if there is one
  • Block the client for some seconds (30 in our setup) waiting for some event to ocurr and change the photograph. If no event was generated at that point, it returns the same photo, only to allow the connection to stay alive and not timeout the client.

This way, when clients polls, it get a response in 0 to 30 seconds max. If a new event was already generated it gets it immediately), otherwise it blocks until new event is generated.

It's basically polling, but it somewhat smart polling to not overheat the webserver. If Comet is not your answer, I'm sure this could be implemented using the same idea but using more extensively AJAX or coding in JSON for better results. This was designed pre-AJAX era, so there are lots of room for improvement.

If someone can provide a actual lightweight implementation of this, great!

Pablo Alsina
+3  A: 

Comet was actually coined by Alex Russell from Dojo Toolkit ( http://www.dojotoolkit.org ). Here is a link to more infomration http://cometdproject.dojotoolkit.org/

Mike Schall
+3  A: 

An interesting alternative to Comet is to use sockets in Flash.

Gilles
+1  A: 

You could try out our Comet Component - though it's extremely experimental...!

Thomas Hansen
A: 

Simplest way:--

<meta http-equiv="refresh" content="10" >

Will direct MOST browsers to refresh the contents after 10 seconds. Your server can then serve up the latest content.

James Anderson
This is a valid method, but it's still polling, not what OP asked for.
Constantin
Not only is this polling, but it's polling by reloading the whole page, not just doing an ajax request in the background.
Mnebuerquo
+1  A: 

You can use a Flash/Flex application on the client with BlazeDS or LiveCycle on the server side. Data can be pushed to the client using an RTMP connection. Be aware that RTMP uses a non standard port. But you can easily fall back to polling if the port is blocked.

Buzzy
A: 

Might be worth checking out Meteor Server which is a web server designed for COMET. Nice demo and it also is used by twitterfall.

Macka
A: 

Might want to look at ReverseHTTP also.

Evan
+12  A: 

Yes, its called Reverse Ajax or Comet. Comet is basically an umbrella term for different ways of opening long-lived HTTP requests in order to push data in real-time to a web browser. I'd recommend StreamHub Push Server, they have some cool demos and its much easier to get started with than any of the other servers. Check out the Getting Started with Comet and StreamHub Tutorial for a quick intro. You can use the Community Edition which is available to download free but limited to 20 concurrent users. The commercial version is well worth it for the support alone plus you get SSL and Desktop .NET & Java client adapters. Help is available via the Google Group, there's a good bunch of tutorials on the net and there's a GWT Comet adapter too.

Nosrama
Definitely the way to go, once you get into implementing it yourself you realize how much there is to do - reconnection, long-polling, streaming iframes, cross-browser support, HTTPS...
Corehpf
An explanation of what Comet is would help this answer
Kevin Monk
@Satir: added a quick explanation. Other answers have links to the Wikipedia article.
Nosrama
will StreamHUb work on the android (the java adapter)?
Faisal Abid
A: 

I haven't looked into it in detail yet, but it was my impression the recently open-source tornado web server from FriendFeed/Facebook has support for this model as it's how FriendFeed works. Written in python. http://www.tornadoweb.org/

+1  A: 

Comet is definitely what you want. Depending on your language/framework requirements, there are different server libraries available. For example, WebSync is an IIS-integrated comet server for ASP.NET/C#/IIS developers, and there are a bunch of other standalone servers as well if you need tighter integration with other languages.

jvenema