Force a 200 header in Ajax while the PHP function is in Sleep

Asked

Viewed 183 times

2

I am creating a chat function, however, the server is waiting for a BD interaction for a period of 10 seconds.

The problem is that while the Ajax connection is open waiting for an answer within 10 seconds, the browser does not accept page switching.

I noticed that Facebook sends a 200 header and keeps the connection open, it makes this kind of problem does not happen.

inserir a descrição da imagem aqui

Does anyone know how I can force a header response?

  • Your code has some line like that? header("HTTP/1.1 200 OK");

  • Yes, but it’s no use, because it only returns the header when the whole script has been run.

  • 3

    And so you are locking a precious thread from the web server for 10 seconds, which means that with few people in the chat your web server will lock whole. You should not hold PHP script running for anything. PHP has to finish the script as soon as possible. If you want to do long Lling, you better choose another technology. As for your chat, try to study websockets, or use AJAX in the way everyone uses: request and quick response. If you have any timer, let it be on the JS side.

  • What technology do you think Facebook is using? I followed the report and it’s doing a 50-second long Polling... the difference is that it can give an answer to HTTP before it completes the 50-second course. After this deadline it closes and opens a new call.

  • 4

    @Rodrigocoelho FB’s PHP is managed by a special engine, and besides it is not only the parser that counts, it is the way you configure the structure. It has nothing to do with Apache + PHP that installs by default on the "go around" machines. Just so you have an idea, see one of the technologies: https://www.facebook.com/note.php?note_id=10150415177928920 - And even so I still think you would solve well with Websockets or AJAX "normal".

  • Right.. So that I don’t go around the same problem, what do you suggest? An example could be stackoverflow actions. Without leaving the screen, it already shows that I had answers. Any suggestions?

  • 1

    Stack Overflow uses websockets for this.

  • 2

    @Rodrigocoelho the OS uses Websockets. But for you not to have to learn a new technology, you can use AJAX by returning a JSON with new conversations every N seconds (too many requests are bad too, but it’s still better than long Polling in this case. When the server "presses", migrate to websockets).

  • Okay, see for tips.

  • 2

    Anyway, now that you understand the idea, follow a pro path that was originally asked: After the header you can send some initial byte sequence, and go using it here: http://php.net/manual/en/function.flush.php. only remember the risks involved. The webserver threads have limited number of simultaneous calls.

Show 5 more comments

1 answer

3

The facebook chat uses an asynchronous web framework made in Python, the Tornado. The concept of asynchronous servers is different, since the whole process is done by stream connections. This concept is also used in Node.js servers (due to the asynchronous nature of javascript).

If your goal is to chat, you can use the Socket io. for PHP Elephant io.. I didn’t use the port for PHP, only the original (from Node.js), but it’s really good at what it does! ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.