6

Suppose I'm synchronizing text on an HTML page, stored in localStorage from computer A to computer B. When the user edits the text on computer A, I send a request to the server, and tell it what text changed. Is there any way that I can get the server to tell computer B that there's new text, without computer B having to keep checking? I've read about Comet, but I'm trying not to use any libraries. Is there a way? Also, if you're going to suggest Comet, could you give a simple example using plain JavaScript and Comet?

I had an idea, but I don't know if it'd work. On computer B, I send a request once, and let the server's file keep delaying the response till there is some new text. That way, it'd be like long-polling, but the request would eventually timeout. If there was a way to set the timeout to be never, this would get much easier. So, anyone have any ideas?

P.S.- I want to use only JavaScript and PHP and try avoid using libraries. Still, if you know of a library that can do this with JS and PHP, please tell me anyway.

Also, I know there have been questions like this, but the answers I found there weren't satisfactory.

Oh, and if anyone knows how the chat over here works, could you tell me that as well?

tl;dr: I want to send a request from a server to an HTML page, or have the HTML send a single request which is kept alive till an appropriate response is created.

Some Guy
  • 14,742
  • 10
  • 54
  • 67
  • This depends are you using HTML5 or not? If so you can use raw sockets to establish a connection with your server from each client, if not, then you will have to have the consumer (computer b) perform continual checks for updates. Also, is this for a production site, a school project, or a personal project? It makes a difference in how you could go about it :) – jdarling Aug 05 '11 at 15:25
  • I am using HTML5 (as I've mentioned I'm using localStorage. :) ). It's for a personal project. Actually a Chrome Extension, so I need it to be reliable. – Some Guy Aug 05 '11 at 15:45
  • As there are implementations of "localStorage" using Flash hacks and additions I didn't make that assumption :). Here are a few links that may be of use to you; http://www.phpclasses.org/package/3213-PHP-Web-based-chat-using-AJAX-to-update-the-dialog.html and http://www.ibm.com/developerworks/web/library/x-ajaxxml8/index.html?ca=drs- These cover PHP, MySQL, and Ajax for basic "chat" applications. Should be a good starting point. – jdarling Aug 05 '11 at 16:53
  • Oh, yeah, I'd forgotten about those hacks. Anyway, thanks for the links. Will check 'em out in a bit. – Some Guy Aug 05 '11 at 16:58

1 Answers1

2

Comet is an implementation of long polling.

The issue is the host machine needs to keep all the resources available for all of the open requests. Servers must be carefully configured to allow this to work smoothly under load. This is not an "out of the box" solution.

Another drawback is each user must be bound to a particular server - so there's no easy way to load-balance requests.

Sticking with periodic polling is often the best bet.

Diodeus - James MacFarlane
  • 107,156
  • 31
  • 147
  • 171
  • Okay. So, is there any way to detect dynamically, how long the poll time should be? Or will I just have to make it a fixed one? – Some Guy Aug 05 '11 at 15:25
  • I've never bothered implementing Comet since it involves a lot of server set-up. See: http://stackoverflow.com/questions/603201/using-comet-with-php – Diodeus - James MacFarlane Aug 05 '11 at 15:40