AJAX optimization - instead of checking every 2 ms?
Heylo guys
i a开发者_运维问答m writing an experimental AJAX page that checks the data of a txt file every 2 ms and if there is a change, post the change to the page
problem is that this is very inefficient bandwidth wise and computation-wise. Is there another way to do this? I have heard of websockets (not familiar though)
thx!
You can use COMET
http://en.wikipedia.org/wiki/Comet_%28programming%29
AJAX Push (Comet server) is exactly what you need. This is a very powerful tookit/engine: http://www.ape-project.org/
Instead of comparing the data, you could only compare the file size/last modified date on your server side script and return a 0/1 depending on if it didn't change or did.
Apart from that you'll have to tell us more about which language you're using on the server for us to be able to tell you what to do. You can use Long Polling or Comet, but it really depends on your server side language how good those would be. With PHP for example, I've found these to not be possible unless I installed stuff on the server, which in shared hosting for example is not possible.
Checking a text file 500 times per second is a bad idea, IMHO.
My suggestion is to have your server-side script check the modified date and "push" a notification to your AJAX listeners. Node.js and Faye would be good for this.
A good screencast on Node can be found here ($12):
http://peepcode.com/products/nodejs-i
That screencast also covers Faye (http://faye.jcoglan.com/node.html). Faye simplifies the whole Comet/polling process.
精彩评论