user1159454
user1159454

Reputation: 3317

Is it possible to trigger AJAX when the file has changed?

I am trying to use AJAX to read when a file or database has changed (had an extra post added to it by another user), and display the newest post (kind of like SO)

And it worked, but the thing is the host I'm using only allows a certain amount of "resource usage per hour" and once the limit is hit, the site is locked out for an hour. This is a free host I'm mainly using for testing and learning.

So before, I had the AJAX set to a setInterval of checking every 2-4 seconds, from a file that was just echoing the last post made in the system. Which I'm guessing is what shut down the site for an hour in a matter of minutes.

So I'm wondering if there's anyway to make it only retrieve the newest post ONLY when the result has changed from what it last found. It sounds like that can't be done because it'd still have to check every time, activating the PHP, regardless of what sends back.

Any ideas how I can do this or something similar?

Upvotes: 0

Views: 136

Answers (1)

Johannes Klauß
Johannes Klauß

Reputation: 11020

You could use http://en.wikipedia.org/wiki/WebSocket (but I guess not on your host, since there is a apache extension you have to install) or you use http://en.wikipedia.org/wiki/Push_technology#Long_polling.

With long polling you send one request to your PHP and the PHP script loops until a new post was found and then send the response.

But you really should consider changing the hoster, because realtime web application need moe ressources. Why not testing and learning locally on your machine?

Upvotes: 1

Related Questions