# REST API async?

**URL:** <https://discourse.slimframework.com/t/rest-api-async/4626>\
**Category:** Questions\
**Created:** [January 26, 2021, 9:26pm UTC](https://discourse.slimframework.com/t/rest-api-async/4626 "2021-01-26T21:26:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![leossmith](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/leossmith/32/1109_2.png) [@leossmith](https://discourse.slimframework.com/u/leossmith)\
**Post date:** [January 26, 2021, 9:26pm UTC](https://discourse.slimframework.com/t/rest-api-async/4626/1 "2021-01-26T21:26:09Z")

</div>

I am writing a rest api in slim to be used by an app i am also developing. While debugging I noticed that in screens that I do 3-4 Rest calls (3 gets) my calls wait for the previous call to complete before they proceed. I am trying to figure out if this is a problem with my Slim app or my mobile app.

So calling the following endpoints:  
domain/rank  
domain/leaderboard  
domain/activity  
domain/user/{id}

They seem to be procedural and not responding as soon as the data is available.

Any help?

ps. I am also attaching an example of an endpoint to make sure that I am not doing something stupid there 🙂

```
/**
 * Get all Activity Posts
 */
$app->get("/activity", function ($request, $response, $arguments) {
    try
    {
        $stmt = $this->get('pdo')->prepare("SELECT posts.*, users.fname, users.lname, users.privacy, 
                                            users.photo_url as user_avatar 
                                            FROM posts left join users
                                            ON posts.userid = users.id ORDER BY posts.created DESC");
        $stmt->execute();
        $count = $stmt->rowCount();
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        if($count==0)
        {
            $status = 404;
            $responseObject = array();
            $responseObject["status"] = $status;
            $responseObject["error"] = false;
            $responseObject["message"] = "Not found";
        }
        else
        {
            $status = 200;
            $responseObject = array();
            $responseObject["status"] = $status;
            $responseObject["error"] = false;
            $responseObject["data"] = $result;
        }

    }
    catch(\PDOException $e)
    {
        $status = 422;
        $responseObject = array();
        $responseObject["status"] = $status;
        $responseObject["error"] = true;
        $responseObject["message"] = "Server error";
        $responseObject["debug"] = $e->getMessage();
    }

    return $response->withStatus($status)
        ->withHeader('Content-Type', 'application/json')
        ->withJSON($responseObject);

});
```

---

<div class="post-metadata">

**Author:** ![maurobonfietti](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/maurobonfietti/32/1114_2.png) [@maurobonfietti](https://discourse.slimframework.com/u/maurobonfietti)\
**Post date:** [January 27, 2021, 3:06am UTC](https://discourse.slimframework.com/t/rest-api-async/4626/2 "2021-01-27T03:06:51Z")

</div>

Hello!

Maybe are you developing with the PHP Built-in Web Server?

> The web server runs only one single-threaded process, so PHP applications will stall if a request is blocked.

[https://www.php.net/manual/en/features.commandline.webserver.php](https://www.php.net/manual/en/features.commandline.webserver.php)

So if for example, you put a **sleep(10)** in your endpoint of GET ‘domain/rank’ and then make another GET request to ‘domain/leaderboard’, the second request it’s blocked until finish the first request.

It’s just an idea… 🙂 🤔

---

<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:** [January 27, 2021, 9:23am UTC](https://discourse.slimframework.com/t/rest-api-async/4626/3 "2021-01-27T09:23:38Z")

</div>

Generally it sounds not good when a web application requires that many requests at the same time.

[For simultaneous requests with Axios, you can use Axios.all()](https://stackoverflow.com/questions/61385454/how-to-post-multiple-axios-requests-at-the-same-time)

Other ideas:

Most browser limit the number of concurrent network calls. [Read more](https://ishwar-rimal.medium.com/why-does-your-browser-limit-the-number-of-concurrent-network-calls-1ae5d50863dd)

When your server starts a **session** it will block all other reqests until its finished. Make sure that you don’t start the session handler. Even if you don’t start it, PHP may start it by default depending on the server configuration.

---

<div class="post-metadata">

**Author:** ![leossmith](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/leossmith/32/1109_2.png) [@leossmith](https://discourse.slimframework.com/u/leossmith)\
**Post date:** [January 27, 2021, 11:54am UTC](https://discourse.slimframework.com/t/rest-api-async/4626/4 "2021-01-27T11:54:49Z")

</div>

It looks like this could be the issue. I am testing locally over XAMPP on my windows pc. For my testing i start the server using composer.phar start and test on that.

I guess this uses the build-in web server?

---

<div class="post-metadata">

**Author:** ![leossmith](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/leossmith/32/1109_2.png) [@leossmith](https://discourse.slimframework.com/u/leossmith)\
**Post date:** [January 27, 2021, 11:57am UTC](https://discourse.slimframework.com/t/rest-api-async/4626/5 "2021-01-27T11:57:19Z")

</div>

The most requests I have for a single screen are 4 which shouldnt be that many to handle.

Can you elaborate a bit more on what you mean by not starting the session handler?

Thank you.

---

<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:** [January 27, 2021, 12:33pm UTC](https://discourse.slimframework.com/t/rest-api-async/4626/6 "2021-01-27T12:33:25Z")

</div>

> composer.phar start

I’m not sure, but this looks you are using the internal webserver of PHP and not Apache.  
In this case the hint from @maurobonfietti was correct. Please check this first and tell us what web server you are using. Try to start the (XAMPP) Apache webserver instead.

---

<div class="post-metadata">

**Author:** ![leossmith](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/leossmith/32/1109_2.png) [@leossmith](https://discourse.slimframework.com/u/leossmith)\
**Post date:** [January 27, 2021, 4:09pm UTC](https://discourse.slimframework.com/t/rest-api-async/4626/7 "2021-01-27T16:09:51Z")

</div>

It looks like @maurobonfietti was correct. The issue was using the build in server instead of xampp. All endpoints waited for the previous to complete instead of running at the same time.

Thank you for all the help.

---

<div class="post-metadata">

**Author:** ![kAlvaro](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/kalvaro/32/730_2.png) [@kAlvaro](https://discourse.slimframework.com/u/kAlvaro)\
**Post date:** [January 29, 2021, 7:09am UTC](https://discourse.slimframework.com/t/rest-api-async/4626/8 "2021-01-29T07:09:38Z")

</div>

> [@leossmith](#):
>
> Can you elaborate a bit more on what you mean by not starting the session handler?

When you execute `session_start()` with the default session handler, PHP locks the session file to avoid it being written by other processes and releases it on `session_write_close()` or script completion (whatever comes first). If you’ve added middleware to handle sessions it’s possible that `session_start()` is running behind the scenes without you even noticing.

The solution is to release the file as soon as you’re done with it (`$_SESSION` values remain after `session_write_close()`, you just cannot change them).

---

<div class="post-metadata">

**Author:** ![kAlvaro](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/kalvaro/32/730_2.png) [@kAlvaro](https://discourse.slimframework.com/u/kAlvaro)\
**Post date:** [January 29, 2021, 7:10am UTC](https://discourse.slimframework.com/t/rest-api-async/4626/9 "2021-01-29T07:10:26Z")

</div>

> [@leossmith](#):
>
> i start the server using composer.phar start

That `start` script should be defined in your application’s `composer.json` file.
