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.
Does anyone know how I can force a header response?
Your code has some line like that?
header("HTTP/1.1 200 OK");
– rray
Yes, but it’s no use, because it only returns the header when the whole script has been run.
– Rodrigo Coelho
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.
– Bacco
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.
– Rodrigo Coelho
@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".
– Bacco
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?
– Rodrigo Coelho
Stack Overflow uses websockets for this.
– bfavaretto
@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).
– Bacco
Okay, see for tips.
– Rodrigo Coelho
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.
– Bacco