This might be a dumb question for those who have already wrapped around him, and maybe I just need more coffee .
Question: Using websockets or ajax it looks like polling is still happening. Is it correct?
Example (not a real project): I want to monitor a text file. If I am missing something (more coffee?), I still have to either a) Ask the server if there is an update, or b) Report the page I have an update; Through sleeping PHP code for a given time or using setTimeout a client-side loop.
Things I understand: I definitely see an advantage already in conversations between the server and the page. I see that I am not sending HTTP requests. Therefore, I see the benefits.
Details: I always used xmlhttprequest , so I decided to check all these things on the network, as from what I thought I understood was that the data is sent to the client in real time, but, as mentioned above, if I don’t see something or some kind of logic here, it seems to me that I still need to either tell php or javascript to check the intervals for the data, otherwise the data is sent in an infinite loop (imagine that you are making a mysql call).
Maybe my logic in my code is all bad. You can view it. Of all the examples I found, each seems to just start an infinite loop in PHP
PHP (minus all connection jargon)
while(true) {
$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 just don’t understand the logic of this in how I can do this in real time without any polling. Perhaps this is how it should work.
I understand that if I delete sleep from php , more and more it turns out in real time, too much, but it looks like it will endlessly try the file in the above example, and this seems wrong.
Edit: To clarify, I'm not specifically looking for a specific solution for viewing a text file. Perhaps you thought about this if you had a question.
: , : , , , .