Leo Jiang
Leo Jiang

Reputation: 26075

How do websites instantly update their content using AJAX?

Today, when I was using Google+ in two separate browsers, I posted something with one browser. The post almost instantly appeared on the second browser (there was maybe 0.5 seconds of delay). How does Google achieve this? Do they constantly send AJax requests to check for new posts? Wouldn't this put a lot of strain on the server?

Upvotes: 1

Views: 245

Answers (4)

Kamyar Nazeri
Kamyar Nazeri

Reputation: 26474

For years google was using Comet or Reverse Ajax: http://en.wikipedia.org/wiki/Comet_(programming))

However, I believe they are using HTML5 WebSocket now that the API is ready: http://en.wikipedia.org/wiki/WebSocket/

http://dev.w3.org/html5/websockets/

Upvotes: 0

Lars Kotthoff
Lars Kotthoff

Reputation: 109232

I suppose one technique they could use is to send an AJAX request immediately and then block it on the server side until a timeout or content is available to be sent.

Upvotes: 0

Calvin
Calvin

Reputation: 8765

Can't say how Google does it exactly for sure, but they would have to be using some sort of push technology. HTML5 WebSockets is something that can do this in newer browsers. In older browsers that don't support websockets, the client usually polls the server periodically. See socket.io for a neat cross-browser implementation of WebSockets, with fallbacks to other methods if the browser doesn't support it, documented here.

Upvotes: 0

tkone
tkone

Reputation: 22728

There are a variety of methods can be used to do this:

  • Websockets
  • AJAX Long-Polling
  • page timers
  • iframes

Each one has it's own caveats and possibilities.

If you're interested in being able to do a real-time application, you might have a look at socket.io which is a great abstraction library for all of these technologies, so it'll use the one which is best supported in your browser.

Upvotes: 2

Related Questions