Greetings to you, dear professionals.
I have a couple of questions.

  1. Header keep-alive ... When the browser sends a request via ajax, the server (apache) does not close the connection for 5 seconds. So, the connection during this time is considered open, and why then when you re-send the browser again forms the headers and stuff? What is the difference? The fact is that if the browser and server do not close the connection for 5 seconds, then why bother with long-polling? What then is the difference between these two queries? In addition to the fact that with a long-polling result will come faster.
  2. In the previous question I mentioned long-polling, although I don’t really know how it works. Example: I have a chat and messages that are stored in the database, that is, the browser sends a request to the server, where the request itself to the database (to check for new messages) is placed in an infinite loop, and if new data is available, then the server responds . I correctly understood the principle? And if the server will not fall from such an "Infinite" number of queries to the database, well, suppose I send a cycle to sleep every 500 milliseconds, but nonetheless. How ideally should this work?
  3. Sockets work on the same principle (I mean checking new data), as I described in the second question?
  4. How long the maximum can last (in time) an AJAX request (version HTTP / 1.1). Well, for example, if you set sleep on the server for 20 minutes. Is it generally normal for so long not to close connections? Does each browser behave differently? I tried the Google Chrome version 38, the connection lasts more than 10 minutes, but I have not tried it for longer.

UPD: Buggy editor here!

    2 answers 2

    The editor is more or less normal :)

    1) with keep-alive in one connection, just a few full-fledged requests. They seem to be "independent." Just the time to establish a connection for small requests takes considerable time. When a long-pulling request goes, and the answer comes later. (in the first case, the server sends the response immediately and does not wait).

    2) The server is not required to poll the database every half a second. Plus various optimizations can be applied.

    And if the server from such an "Infinite" will fall the number of queries in the database

    and it depends on your code. The base can lie on a single request. And it can serve thousands of requests per second.

    3) everything is simpler with sockets. Both keepalive and longpolling work on top of http (usually). And http works on top of sockets. Therefore, through the sockets you can do the above, and make other methods.

    four)

    How long the maximum can last (in time) an AJAX request (version HTTP / 1.1).

    In theory - infinitely. I did not see restrictions

    Well, for example, if you set sleep on the server for 20 minutes. Is it generally normal for so long not to close connections?

    Yes, if necessary.

    Does each browser behave differently? I tried the Google Chrome version 38, the connection lasts more than 10 minutes, but I have not tried it for longer.

    Yes, this is all very browser dependent.

    • Thanks for the response! I have some thoughts on this issue, I’ll need to experiment now) - Hit-or-miss
    1. Keep-alive in HTTP / 1.0 saves the client time to raise the connection, and the server - resources to accept the connection. Different servers give a different timeout to Keep-Alive .
    2. Yes, there is some kind of "infinite loop". But you do not mind that all running programs on your computer are in an infinite loop. And there is no need to pull the database continuously. And not necessarily the current chat messages in general to keep in the database.
    3. Yes, the general scheme is as follows. Data reading -> Reaction.
    4. Again, different browsers have different default timeouts.

    Ps. The editor is not buggy. Just in the first version of the post you did not observe the markdown Markdown and, apparently, did not look at the preview.

    • Thanks for your reply. With these sockets in general got confused. I will try to implement on PHP sockets and HTML5. - Hit-or-miss