This may be a silly question to some who have wrapped their head around it already and maybe I just need more coffee.
Question: Whether using websockets or ajax it seems like there is still some polling happening. Is this correct?
Example (not real project): I want to keep an eye on a text file. Unless I am missing something (more coffee?), aren't I still having to either a) Ask the server if there is an update, or b) Tell the page I have an update; Through either sleeping the PHP code for a set time or having a setTimeout loop on the client side.
Things I do understand: I definitely see a benefit already with talking back and forth between server and page. I see that I am not sending http requests. So I see benefits.
Details: I have always just used xmlhttprequest so I decided to check out this whole websockets thing as from what I thought I understood, is that data is sent to the client in real time, but, like stated above, unless I am missing something or some logic here, it seems like I still have to either tell php or javascript to check in intervals for data, otherwise data is being sent in an endless loop (imagine making a call to mysql).
Maybe my logic in my code is all kinds of bad. You are welcome to view it. From all of the examples I found, everyone seems to just run an infinite loop in PHP
PHP (Minus all of the connection jargon)
while(true) {
// update once a second
$this->send($client, file_get_contents('/my/file/test.txt'));
sleep(1);
}
Javascript
var websocket = new WebSocket( "ws://mysite.com:12345" );
websocket.onmessage = function( str ) {
console.log( str.data );
};
I am just not grasping the logic on this on how I can make it real time without some sort of polling. Maybe this is how it is supposed to work.
I do understand that if I remove the sleep from the php code things get much more real time, too much, but this seems like it would infinitely poll the file in the example above and that doesn't seem right.
Edit: To clarify, I am not specifically looking for a specific solution to watching a text file. You may have thought this if you skimmed the question.
Edit: Future visitors, the answer to this is: instead of specifically watching for changes, when a user sends a change in, you send the change to open connections.