0

The MVC (model-view-controller) framework is used in the application. The client (an iOS App) and the server use the http protocol to communicate.

The function is called upload_data which is defined in the model file named profile.model.php. When this function is called, I would like it to simply return a successful message to the client first, and then calls another function process_data to handle time-consuming tasks such as reading data from and writing data to the database.

<?php
      function upload_data($data){
          // simply returns a successful message to the caller
      }

      function process_data($data){
         // handles time-consuming tasks
      }
?>

I just wonder what is the best way to do it. Thanks.

Kofi Black
  • 111
  • 1
  • 7
  • PHP process everything in server side and will send info to client only after every server side activities requested by client are completed. If you want to override this behavior, go for the AJAX call. Initially call upload_data, then, create a synchronized call to process data – Thamilhan Apr 17 '17 at 08:45
  • @Thamilan With all due respect, but that's rather nonsensical. – deceze Apr 17 '17 at 08:46
  • @deceze, once a function returns a value to client, is there any other chance it can call another function? – Thamilhan Apr 17 '17 at 08:48
  • @Thamilan A server isn't limited to act only upon client request; a server can perfectly respond to the client *and then continue to do other stuff*. It's not exactly PHP's default MO to operate this way, but it can be done. – deceze Apr 17 '17 at 08:50
  • @deceze, seriously, I didn't know that. I just presumed in the case of OP that client is waiting for the reply :) – Thamilhan Apr 17 '17 at 08:52

1 Answers1

0

I think you want to send two response to client .. on single request. First you want to send success response and then process the data, and after processing you need to send rest of the data.

If so then you need to establish a websocket connection with the webserver. Using websockets you can send any number of response with any amount of time gap. Its basically a consistent connection between your app and webserver.

Ratchet is a good library to implement web-sockets using php.

Atul Sharma
  • 9,397
  • 10
  • 38
  • 65