4

I have a series of XML files which can be retrieved, edited and saved by a User. My intention is to allow multiple Users to edit these files at the same time. Many parts of these XML files relate to content displayed in the browser UI for example a <name>My title</name> node is displayed and can be edited.

The technologies I'm using are Javascript, PHP, and a master XML file containing references to other XML files (both master and referenced files can be edited in the UI). The server is WebDAV enabled, and WebDAV methods are used via YUI3's io module to handle retrieval, saving, collection moving etc.

How do I go about updating UIs where these resources are being used, based on the contents of the edited and saved XML file(s)?

I know I could probably run setTimeouts and whatnot to check for updates, but it seems more intuitive to make the UI respond only when data is changed.

cheers!

danjah
  • 2,939
  • 2
  • 30
  • 47

1 Answers1

4

The feature you're describing is similar to a technique known as server-push. What you're asking to do is a very tricky thing for a web app (especially for PHP, which is built around the idea of a request that gets served and the script terminating).

HTML5 is introducing technologies such as websockets for maintaining a persistent connection to a server, you could look into websockets as a solution, but it's a brand-new technology and I don't think the spec is even finalized yet, so it will only be implemented in the very latest versions of browsers, if at all.

You've already mentioned AJAX polling (driven by setInterval), but you've also noticed that it's problematic. You're right of course, it is, local data can become stale in the interval between polls, and you'll generate a lot of traffic between the server and any open clients.

An alternative is so called "long-polling". The idea is the client starts an AJAX session with the server. On the server the script invoked by the client basically just sits there and waits for something to change. When it does, the server notifies the client by sending a JSON/XML/whatever response and closing the AJAX session. When the client receives the response, it processes it and initiates a new AJAX connection to wait for another server response.

This approach is almost instantaneous, because data gets pushed to the client as soon as it's available. However, it also means lots of open connections to the server and this can put the server under a lot of load. Also, PHP scripts aren't really meant to run or sleep for a long time due to the request-response model the language is built around. It is possible, but probably not advisable to follow this approach.

How do I implement basic "Long Polling"? has some examples of the long-polling technique.

Good luck!

Community
  • 1
  • 1
GordonM
  • 31,179
  • 15
  • 87
  • 129
  • You might also want to look at http://en.wikipedia.org/wiki/Comet_(programming) which describes the long polling (aka COMET) technique in more general terms – GordonM Feb 29 '12 at 11:01
  • Fantastic info, thanks GordonM. I've read up on those links and several spiraling resources - I knew there must be a term for what I was trying to do. "Long polling" and "Comet" lead me to a YUI3 Gallery module that purportedly handles the Long Polling via EventSource with an IE fallback. FWIW, my project doesn't need to scale past 25 users for a good while. And is this sort of functionality what Node.js servers are good at? – danjah Feb 29 '12 at 11:30
  • Can't really comment on node.js as I've never used it. However, given that it's based on javascript and therefore has an event model, I'd imagine it would be better able to cope. As I'm sure you've read from the links, it's also the Apache resources that get tied up with each request that you have to worry about. – GordonM Feb 29 '12 at 14:40
  • Yeah, I'm not quite ready to move away from Apache yet, but maybe one day :) Is there a general resource use 'rule of thumb' for a basic web server not intended for mass production? Should I be worried about 20, or even 50 users using long polling? – danjah Feb 29 '12 at 20:40
  • I doubt 50 users would hurt too badly. – GordonM Feb 29 '12 at 21:07
  • Gidday Gordon - I was wondering if you might be able to shed some light on this SO question: http://stackoverflow.com/questions/9565290/how-to-fire-eventsource-sse-events, it relates to a YUI normalised implementation of EventSource, would be greatly appreciated if you had any input :D – danjah Mar 06 '12 at 21:27