Flush response early and post process operations

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();
            }
        
        }
    }
?>

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

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

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:

1 Like