Skip to content
Advertisement

Can php wait for a very long time before responding to an ajax request

I would like to make a chat site where the js AJAXs to PHP and the PHP waits for the database to change from what the user has then it responds to the user and the user again in the recall function AJAXs the server and so on. I would like to call the same server holding the request to add any chat that the user wants to add. I would like to know if this is possible.

Advertisement

Answer

Yes, you could but that doesn’t mean you should.

Just for the idea, you can make a loop with sleep() inside the loop, and also a script that checks for changes in the database, once the database has changed then you return your message and get out of the loop. However this will leave your server with lots of processes and connections.

An alternative is to use websocket which is designed for real-time messaging and more suited for this task.

Advertisement