59

All HTTP responses require the client to initiate them, even those made using AJAX. But GMail's chat feature is able to receive messages from other users, even when I'm just sitting in my comfy computer chair watching but not interacting with the browser. How did they do it?

Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
chat
  • 1,789
  • 3
  • 14
  • 11
  • 8
    Question hijack: what kind of AJAX polling strategy does Gmail use to implement the chat client? – Jimmy Apr 09 '09 at 03:56
  • wow you practically re-wrote the question. – Pablo Fernandez Apr 09 '09 at 04:06
  • @Pablo: yeah... pretty sure this is what he was asking, but didn't figure it out 'till i'd already answered, so i wanted to clean it up 'fore someone else made the same mistake. – Shog9 Apr 09 '09 at 04:08
  • @Pablo: Yes he did rewrite the question. It made zero sense in the original post. – Samuel Apr 09 '09 at 04:12
  • This Comet thingy sounds like the right answer, but I have also done something where a jQuery loop just keeps checking the server for updates, like chat updates. No user intervention required. Still it seems there is existing technology for this, not worth re-inventing. – Tim Nov 01 '11 at 20:06

3 Answers3

42

That tech is known as "comet", but also as "server push", "reverse ajax", etc.

It's about pushing data from the server to the browser, keeping an http connection alive. Find more info on it on the wikipedia article (English version).

Also here's a pretty good presentation with Joe Walker from DWR, where he talks about comet.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
  • do you know which method Gmail uses? Hidden IFrame, XMLHttpRequest? – chat Apr 09 '09 at 04:11
  • Perhaps link to the English version of Wikipedia? – Frank Crook Apr 09 '09 at 05:37
  • @Frank Sorry! My locale betrays me :D, thanks jon for the correction, I'll double check next time. – Pablo Fernandez Apr 09 '09 at 16:04
  • 1
    I haven't looked at comet in awhile, but last time I looked into it the issue was that apache really can't hold that many connections open at once. So if you were to implement comet you would need a lot more servers then you would otherwise need. – thirsty93 Apr 09 '09 at 17:22
  • 1
    @thirsty in the presentation linked above, the author states that when apache2 was under development, comet was not as known and therefore it's support is poor. I believe this will improve in next releases if comet caches up a bit more. – Pablo Fernandez Apr 09 '09 at 20:29
  • 1
    @RexM: Would be nice if it was recorded here to make life easier for everyone. – Brian MacKay Oct 03 '11 at 03:04
8

As you rightfully pointed out, HTTP requires data to be 'pulled' by the client. Gmail can still 'pull' data from the server by using a timer to trigger the HTTP operation instead of requiring the user to click something. So, it may seem to be auto, but it is still client initiated.

sybreon
  • 3,128
  • 18
  • 19
  • 1
    I've heard they use comet. It's not a client request AFAIK – Pablo Fernandez Apr 09 '09 at 03:59
  • 1
    I see. Learned something new today. Thanks! – sybreon Apr 09 '09 at 04:30
  • 7
    Comet is still client-initiated AFAIK - it's just that the server keeps the connection open until it's got something interesting to say instead of immediately returning a response. – Jon Skeet Apr 09 '09 at 06:13
  • 1
    And indeed reading through the wikipedia article, it's pretty clear that for all current implementations, the client initiates the connection. It's hard to see how else it could work, to be honest. – Jon Skeet Apr 09 '09 at 06:16
  • 1
    I guess it depends on how it is viewed. At the TCP layer, it is definitely client pull. However, at the app layer, it depends. If it sends something down that triggers a javascript in the client, is it still client pull? If it immediately sends another trigger, is it still client pull? – sybreon Apr 09 '09 at 11:28
5

Yep Comets is correct. Google Web Toolkit Applications by Ryan Dewsbury explains how to create a Comets based Instant Messenger application in chapter 9.

Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
Ankur
  • 50,282
  • 110
  • 242
  • 312