Is there some way to PUSH data from web server to browser?

AjaxWebserverComet

Ajax Problem Overview


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?

Ajax Solutions


Solution 1 - Ajax

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.

Solution 2 - Ajax

Yes, it's 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 it's 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 for free but is 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.

Solution 3 - Ajax

Nowadays you should use WebSockets. This is 2011 standard that allows to initiate connections with HTTP and then upgrade them to two-directional client-server message-based communication.

You can easily initiate the connection from javascript:

var ws = new WebSocket("ws://your.domain.com/somePathIfYouNeed?args=any");
ws.onmessage = function (evt) 
{
  var message = evt.data;
  //decode message (with JSON or something) and do the needed
};

The sever-side handling depend on your tenchnology stack.

Solution 4 - Ajax

Look into Comet (a spoof on the fact that Ajax is a cleaning agent and so is Comet) which is basically "reverse Ajax." Be aware that this requires a long-lived server connection for each user to receive notifications so be aware of the performance implications when writing your app.

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

Solution 5 - Ajax

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.

Solution 6 - Ajax

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!

Solution 7 - Ajax

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

Solution 8 - Ajax

Yet another, standard, way is SSE (Server-Sent Events, also known as EventSource, after the JavaScript object).

Solution 9 - Ajax

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/

Solution 10 - Ajax

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.

Solution 11 - Ajax

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.

Solution 12 - Ajax

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.

Solution 13 - Ajax

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

Solution 14 - Ajax

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.

Solution 15 - Ajax

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

Solution 16 - Ajax

please check this library https://github.com/SignalR/SignalR to know how to push data to clients dynamically as it becomes available

Solution 17 - Ajax

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

Solution 18 - Ajax

Might want to look at ReverseHTTP also.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNiyazView Question on Stackoverflow
Solution 1 - AjaxGreg HurlmanView Answer on Stackoverflow
Solution 2 - AjaxNosramaView Answer on Stackoverflow
Solution 3 - AjaxpafindeView Answer on Stackoverflow
Solution 4 - AjaxDan HerbertView Answer on Stackoverflow
Solution 5 - AjaxJerod VenemaView Answer on Stackoverflow
Solution 6 - AjaxPablo AlsinaView Answer on Stackoverflow
Solution 7 - AjaxGillesView Answer on Stackoverflow
Solution 8 - AjaxᅙᄉᅙView Answer on Stackoverflow
Solution 9 - AjaxMike SchallView Answer on Stackoverflow
Solution 10 - AjaxKibbeeView Answer on Stackoverflow
Solution 11 - AjaxMarkus OlssonView Answer on Stackoverflow
Solution 12 - AjaxBuzzyView Answer on Stackoverflow
Solution 13 - AjaxBigJumpView Answer on Stackoverflow
Solution 14 - AjaxsparkesView Answer on Stackoverflow
Solution 15 - AjaxThomas HansenView Answer on Stackoverflow
Solution 16 - Ajaxarun peddakotlaView Answer on Stackoverflow
Solution 17 - AjaxRyan AhearnView Answer on Stackoverflow
Solution 18 - AjaxEvanView Answer on Stackoverflow