Flush response early and post process operations


#1

My API call is taking too long to complete as after operation completion it calls php_curl to extenal server for sending SMS and another extenal SMTP server to send Email and I might integrate WhatApps API to send message in future.

What I want to do is send / flush response once operation completes
and Do the post processing to send SMS/ Email / Whatsapp later.

<?php
    use \Psr\Container\ContainerInterface as ContainerInterface;

    class SomeController
    {
        protected $container;
        public function __construct(ContainerInterface $container) {
            $this->container = $container;
        }

        public function save($request, $response, $args) {
            $dataService = new SomeService($request);
            $dataService->saveSome();
            $opResponse = $dataService->getReponse();
            // HOW TO ACHIEVE THIS IN SLIM
            $opResponse->flush()->end();


            if($opResponse->succes = TRUE){
                $smsService = new SMS($opResponse);
                $smsService->sendSaveSMS();

                $mailService = new Email($opResponse);
                $mailService->sendSaveEmail();

                $waService = new Whatsapp($opResponse);
                $waService->sendSaveWhatsapp();
            }
        
        }
    }
?>

#2

You could store this task into a “queue” and process it later.


#3

Any url / guide / docs to implement queue with slim?


#4

A queue is out of Slims scope and more “DevOp”.

Here are some links about this topic:

You need the following components and tools to implement a asynchronous message system:

I hope this helps you. :slight_smile: