# Flush response early and post process operations

**URL:** <https://discourse.slimframework.com/t/flush-response-early-and-post-process-operations/2787>\
**Category:** Questions\
**Created:** [November 7, 2018, 5:51am UTC](https://discourse.slimframework.com/t/flush-response-early-and-post-process-operations/2787 "2018-11-07T05:51:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anupuidev](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@anupuidev](https://discourse.slimframework.com/u/anupuidev)\
**Post date:** [November 7, 2018, 5:51am UTC](https://discourse.slimframework.com/t/flush-response-early-and-post-process-operations/2787/1 "2018-11-07T05:51:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [November 7, 2018, 8:02am UTC](https://discourse.slimframework.com/t/flush-response-early-and-post-process-operations/2787/2 "2018-11-07T08:02:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anupuidev](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@anupuidev](https://discourse.slimframework.com/u/anupuidev)\
**Post date:** [November 7, 2018, 11:40am UTC](https://discourse.slimframework.com/t/flush-response-early-and-post-process-operations/2787/3 "2018-11-07T11:40:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [November 7, 2018, 1:32pm UTC](https://discourse.slimframework.com/t/flush-response-early-and-post-process-operations/2787/4 "2018-11-07T13:32:44Z")

</div>

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

Here are some links about this topic:

- [https://matthiasnoback.nl/tags/command%20bus/](https://matthiasnoback.nl/tags/command%20bus/)
- [http://docs.simplebus.io/en/latest/](http://docs.simplebus.io/en/latest/)
- [https://github.com/geggleto/slim-queue](https://github.com/geggleto/slim-queue)

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

- A library to send and receive messages to/from other applications or via message queues.
  - [https://symfony.com/doc/current/components/messenger.html](https://symfony.com/doc/current/components/messenger.html)
  - [https://github.com/geggleto/slim-queue](https://github.com/geggleto/slim-queue) \> [Example](https://github.com/geggleto/slim-queue/tree/master/example)
  - [https://github.com/queue-interop/queue-interop](https://github.com/queue-interop/queue-interop)

- A message queue storage like [RabbitMQ](https://www.rabbitmq.com/) or any other [implementation](https://github.com/queue-interop/queue-interop#implementations).
  - for simple use cases MySQL is still Okay.

- A running consumer process
  - crontab (max 1x per minute) or
  - [Supervisor](http://supervisord.org/) if you want to consume more then one command per minute.

I hope this helps you. 🙂
