Answer the question
In order to leave comments, you need to log in
Realtime message output (JS + PHP)?
How to organize the output of messages to the browser in real time? Let me explain in more detail: there is a php script that does some long operations with the database. The user types his address in the browser line, presses Enter, and calmly smokes for 30-40 minutes. But I would like him to see progress, I would like to display him messages about the operation for each position
[42 / 89300] Позиция #327 найдена, существует в системе, данные актуальны.<br/>
[43 / 89300] Позиция #21 найдена, существует в системе, данные актуальны.<br/>
[44 / 89300] Позиция #532 найдена, в системе не обнаружена, добавленна только что.<br/>
Answer the question
In order to leave comments, you need to log in
I would do so. An additional page where messages are displayed. i.e. the script works and writes messages to the database or to a file as it is more convenient, and from this page they are already displayed. Then I make a loop from Ajax that picks up messages and displays them on the screen.
You can try using the META REFRESH tag with an interval of a few seconds/minutes
You are almost right - you need a periodic flush on the server side.
How can I force the server to send responses in gzip chunks?
Either you can change the content-type to one that won't be caught by Apache and won't crash, or you can simply ask for a chunked data transfer option.
I would personally organize it as follows:
1) The dispatcher page is static, it loads quickly. Upon loading, it sends an ajax request to execute your long-running script (or you can send a request as a submit to an iframe)
2) The script queues the generated messages into a database table or text file as it executes.
3) From the dispatcher page, by a periodic ajax request, we receive ready-made new messages from the queue.
flush won't help much. We need a separate message transfer protocol - whether comet, websocket or ajax - the tenth question, the main thing is how to do it.
For a large number of small operations such as loading strings into the database, I simply made INSERT packets with an ajax script - marked progress in the client and took the next block in a cycle. This approach requires additional operations such as saving / restoring the content of the last operation if it failed and a correct rollback, and so on.
For long operations, such as requests over the network, I made a background process that took a job from the queue and executed it. And the ajax client simply displayed the status of the queue.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question