K
K
Kirill Sidorov2011-07-06 07:13:21
PHP
Kirill Sidorov, 2011-07-06 07:13:21

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/>

in that spirit.

But here there are several questions, the answers to which I have not found.
  1. You can use flush in php to clear the output buffer, but Apache with gzip will still cache, how to turn it off programmatically - I have no idea.
  2. If you use AJAX, then as I understand it, the callback function will work only when all the data arrives, it’s also not clear how to display it.

Can anyone suggest which way to dig?

Answer the question

In order to leave comments, you need to log in

9 answer(s)
S
Sergey Beresnev, 2011-07-06
@sectus

comet

A
Anton, 2011-07-06
@AntonMinsk

Take a look at dklabrealpolexor , a fairly lightweight comet server.

A
Anton Pronin, 2011-07-06
@nightw0rk

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.

K
Kindman, 2011-07-06
@Kindman

You can try using the META REFRESH tag with an interval of a few seconds/minutes

A
Anton Korzunov, 2011-07-06
@kashey

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.

F
Fastto, 2011-07-06
@Fastto

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.

G
g00d, 2011-07-06
@g00d

You can also use websockets

W
Wott, 2011-07-06
@Wott

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.

B
Borro, 2011-07-07
@Borro

Actually, a post about WaterSpout just recently flashed - a web server for asynchronous data transfer, I think this is what you need. You just have to tweak it for yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question