views:

94

answers:

6

I'm trying to write a chat client using javascript, but I'm being hindered by only being able to communicate with the server using XMLHttpRequest which as far as I can tell is completely unsuitable, as many have told me.

I've looked for other networking functions but haven't found any and been told that there aren't any. It's almost enough to make me stop looking, except Gmail seems to be doing it somehow. It makes no requests for several minutes, then as soon as I send an email to myself, my inbox in another window sends a POST request and receives the email. Since it hasn't sent any http requests to the server for minutes (I checked using firebug) it can't have been talking to the server that way, so how does it know when it has an email to receive?

A: 

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

Jon List
Comet starts and ends with HTTP.
Rob
+1  A: 

Gmail uses a variant of Comet approaches. I'm not sure of the specifics, but I believe it is some sort of long-poll running in <script> tags or an <iframe>. The Wikipedia article on Comet has more detail.

bcherry
A: 

GMAIL does use HTTP. The only difference is that they don't do polling, but I believe tags http-streaming. It keeps the connection open instead of closing it and trying again.

You did not specifiy what kind of server architecture you are using, but You could also use BOSH(for example prosody does have BOSH) to achieve this. Strophe.js is a real clean javascript library to achieve this.

Alfred
A: 

They use Comet. Further reading -> http://www.ekhoury.com/blog/2007/02/07/ajax-alternatives-gmail-choice/

mellowsoon
A: 

You might consider WebSockets.

Advantages:

  • Much less overhead than HTTP (or AJAX/Comet)
  • Easy to implement on the client side.
  • Closely related to HTML5, so using it gets you "bonus points". :-)

Disadvantages:

  • Requires a browser with WebSockets support or Adobe Flash support: web-socket-js project provides a fallback Flash emulator.
  • You need something on the server side that supports WebSockets.

For the back-end check out pusherapp which handles the back-end of the type of application you are wanting to create. You get 5 simultaneously clients and 10,000 messages per day for free.

Here is a google intro to WebSockets: http://blog.chromium.org/2009/12/web-sockets-now-available-in-google.html

Also, check out wsproxy which is a WebSockets to generic TCP sockets proxy included with noVNC (HTML5 VNC client). noVNC has a C and python version of wsproxy. Disclaimer: I created noVNC.

kanaka
+2  A: 

I asked a similar question a while back, take a look at it there are pretty nice answers.

http://stackoverflow.com/questions/1113661/how-can-i-start-ajax-push-website-activemq-or-cometd-or-sth-else

Sinan Y.