Getting an AJAX response and continuing to process...

83 views
Skip to first unread message

Steven Berkson

unread,
Mar 14, 2024, 3:23:39 AMMar 14
to joomla-de...@googlegroups.com

Does anyone have any advice for trying to get start a mailing process through an ajax request and close the connection but have the script continue to process through the Joomla framework?

 

I’m working on part of my project to message significant numbers (100-600) existing customers through the backend at a time where the group to be messaged is configured by a variety of criteria. What I am trying to achieve would go as follows. After selecting the group to message and vetting the data to make sure everything is in order, the mailing process calls an ajax controller function which uses the appropriate model to do a final check then sends a response like, “Your message is being sent.” Then closes the connection but proceeds to call the helper function that sends the message. A separate ajax call to a different function in the ajax controller returns feedback on the progress.

 

I have it working, but only if the first ajax call stays open, so I cannot get any response from the first call until the process is over.

 

Data => Ajax Call => Ajax Controller (function start) => Model => Process the data and gives the thumbs up => Ajax Return Response (“Process Started”) => Ajax Controller => EmailHelper gets Data and Sends individual emails serially.

 

Separate Ajax Call => Ajax Controller (function status) => Model => Returns status information on request.

 

I’ve tried various versions of this without success. I reviewed the JsonResponse, which I was using) and tried to do this without that for the response to eliminate variables. I tried closing the application, but that stops all processing. I can’t figure out a way to trigger the send function after closing the connection within the framework.

 

I have achieved the result by simply expecting no response from the first call and getting the feedback I need from the second ajax call, but it would be nice to be able to close the first call if I could.

 

 

This is the kind of code that does not seem to be working inside the framework.

 

AjaxController Class…

 

public function sendMessage() {

 

…(error checking)…

 

if (!($sendData['errors'])) {

 

set_time_limit(43200);

              ob_start();

              echo '{"success":true,"message":null,"messages":null,"data":{"sending":true,"status":"Your Messages are Sending!"}}';               

//echo new JsonResponse($resp); // Removed in favor of line above to see if the JsonResponse function was interfering

              $size = ob_get_length();

              header('Content-Type: application/json');

              header("Content-Encoding: none");

              header("Content-Length: {$size}");

              header("Connection: close");

              ob_end_flush();

              ob_flush();

              flush();

              if(session_id()) session_write_close();

              if (function_exists('fastcgi_finish_request')) {

                  fastcgi_finish_request();

              }

              //$this->app->close();

              $sendData['messageData'];

EmailHelper::sendMessage($sendData);

              die();

 

Reply all
Reply to author
Forward
0 new messages