1

I am trying to make a web chat application which interacts with gtalk. was successful in sending messages to the gtalk user from my app. I am able to receive the message on to my server also if the gtalk user pings me from the other end, but the problem is how do I know that there is a message waiting. I could only think of an ajax hit every 5 secs or something. Is there a better way to implement it?

Thanks for your time.

DOK
  • 31,405
  • 7
  • 58
  • 91
nandu
  • 769
  • 4
  • 18

3 Answers3

1

As far as I can see what you really need here is one of the Comet solutions. Your choice depends on your application architecture and browsers you plan to support.

If it is ok for you to support mainstream browsers only I would suggest to use web sockets for this, here is a quick demo - http://html5demos.com/web-socket.

Joseph
  • 107,072
  • 27
  • 170
  • 214
Sergey Rybalkin
  • 2,934
  • 21
  • 25
  • It's [not yet totally mainstream](http://caniuse.com/#search=socket), unless you target firefox and chrome only – Joseph May 26 '12 at 18:17
  • 1
    Just tried and looks like they are using web sockets with the fallback to script tag long polling in IE, but I'm not sure. – Sergey Rybalkin May 26 '12 at 18:34
1

According to your question, I think you're searching something like a push mail system, that is, you are trying to get a push from server to browser. But,I don't think there is any better way than an ajax hit every 5 seconds.

There is a way with Comet, it doesn't do the trick so well. See here

But, in HTML5 you have socket. you can get more about socket from following links:

http://en.wikipedia.org/wiki/WebSocket

https://stackoverflow.com/questions/4262543/what-are-good-resources-for-learning-html-5-websockets

So considering all aspects, it will be better to go with your choice.

Community
  • 1
  • 1
thecodeparadox
  • 81,835
  • 21
  • 131
  • 160
0

Prior to HTML 5, nope. You have to poll.

There are several reasons for this (which all boil down to how the web, web browsers, and HTTP work), but the easiest way to think about is that the web and HTTP function on a Request/Response pattern; that is, a request from the web browser is sent to a web server that returns a response, and then the connection is severed. The server has no way of reestablishing contact with the browser; the browser has to reach back out to the server.

KP Taylor
  • 2,080
  • 1
  • 17
  • 15