2

Say that we have an Ajax call. This for just an example:

  function delete_mov(id){
    $.ajax("delete_mov.php?id="+id
    )
    .success(function(){
      $('#message').("Movimento "+id+" correttamente cancellato");
    })
    .fail(function(){
      $('#message').("Problemi a cancellare il movimento "+id);
    });
  }

It will do its job as expected. Say that the PHP behind will take some time to be executed and that the php code sends some messages when it will complete a step of the execution. For example:

<?php
//do first part of code
echo "First step completed";
//do second part of code
echo "Second step completed;
//do third part of code;
echo "Third step completed;
//do last part of code
//send success message back to Ajax
?>

What I'd like to do is to receive back from the ajax call each message when it is echoed by PHP so that the message div will be updated with each message one after the other. Is it feasible? Is it possible to send some messages between request and response?

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
  • I believe PHP will execute all of it and send it together. How long does the ajax call takes? – akshay khairmode Jun 15 '18 at 15:00
  • it will take enough time to see each message one after the other – Lelio Faieta Jun 15 '18 at 15:01
  • 2
    You can use 2 ajax calls. One which does the Job. Second which will check where the code has reached. You can create a logic which will put data in a session variable. During checking you can check that session variable for the current status – akshay khairmode Jun 15 '18 at 15:03
  • The question is if it is feasible to do it in the same Ajax call. I am not looking for workarounds. – Lelio Faieta Jun 15 '18 at 15:04
  • 2
    No. You cannot do it in a single AJAX call. You'd need to make the main call, then have separate AJAX calls which go to another page which reports on the state of the first request (so you will need some method of identifying that action). Websockets may work better for this if it's a very long running action – Rory McCrossan Jun 15 '18 at 15:07
  • As far as I know you can only get one response from an ajax call, which is sent after the php code is done running. What you could do is to fire another ajax call after receiving the response from the first one. – Barskey Jun 15 '18 at 15:07
  • websocket is what I am currently using. So i'll keep the current architecture – Lelio Faieta Jun 15 '18 at 15:10

1 Answers1

2

All of the comments really ignore the fact that HTTP server can send Streamed Response back to client.

Several of PHP frameworks have this implemented already (Symfony, Laravel, etc...). If you are eager to work without one, that is certainly possible and you can start with making sure that

  1. Your PHP back-end flushes output buffer more frequently (via output buffering) and not all at the end:

    ob_start();
    echo "First";
    ob_flush();
    sleep(1);
    echo "Second";
    ob_flush();
    sleep(3);
    echo "Final";
    ob_flush();
    

Thing get a little bit complicated when you use some FastCGI. Proxy server may choose to buffer on its own thus render your streamed response as a normal one. In this case, you may want to include flush() call right after each ob_flush(). In addition, you may also be required to send additional headers which hint server to cease buffering.

  1. Your JS front-end utilizes progress callback. See this question for an example: What is the cleanest way to get the progress of JQuery ajax request?

    $.ajax({....})
        .done(function(){ ....... })
        .fail(function(){ ....... })
        .progress(function(value) {  
            console.log('Progress: ', value + "%");
        }
    

But if you already have a websocket architecture in place, that may be more suitable in general terms.

Hope this helps a bit...

Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85