Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

orest's avatar
Level 13

stream data for performance

I have a situation where a large amount of data needs to be fetched from the database and then sent through HTTP, and i am looking for a way to improve the performance of it.

I am considering streaming the data but,

  1. I don't know how to properly do that
  2. i don't know if it is meant for this kind of problem.

To give an example, i have 2 apps.

App 1

The App 1 will get the data from the database, and it will return a stream response.

Route::get('/stream', function(){
     return response()->stream(function() {
            Items::query()->chunk(200, function($items){
                echo json_encode($items);
            });
        });
    },  200, [
        'Content-Type' => 'application/json',
    ]);

App 2

$response = $app1->get('/stream');

i want to get the data as models and not string

  • If i do $response->getContent() it returns false
  • if i do $response->streamedContent() it returns a string with all the data
0 likes
11 replies
Sinnbeck's avatar

You cannot respond with a model as such. You can send string that the other end can parse.

Perhaps explain why it needs to be sent through Http (and to where) and why you need it to send models?

orest's avatar
Level 13

@Sinnbeck

The idea is to improve performance so that is something i want to try. So there is no particular reason to send it through HTTP.

As i described, i have 2 apps and one of the apps needs data from the other. Having access to the same db is not an option. Also taking into consideration that the amount of data is large, i thought that maybe i could stream the data.

Also i can't parse the data. If i try to do json_decode() it returns null. Do you mean to parse it directly from the string ?

orest's avatar
Level 13

@Sinnbeck

i want also to try to minimise the number of requests. And with streaming this can be achieved.

orest's avatar
Level 13

@Sinnbeck

Is there a way to return json from the stream and to be able to do json_decode on the other end ?

orest's avatar
Level 13

@Sinnbeck

Currently i do echo json_encode($items) in App 1

But in App 2

  • if i try to do json_decode($response->streamedContent()); i get null. It seems that the string that is returned from App 1 is not a valid json
Sinnbeck's avatar

@orest you said that this gave you the data? $response->streamedContent()?

orest's avatar
Level 13

@Sinnbeck

$response->streamedContent() returns the data as a string, but that string is not a valid json

json_decode($response->streamedContent()); returns null

orest's avatar
Level 13

@Sinnbeck

the json-last-error is 4

The problem is that if i do echo within a foreach or inside chunk() then for some reason the json format is messed up.

for example if i do the following, The json format is messed up

foreach($items as $item)
{
     echo json_encode($item);
}

But if i do the following ( echo without foreach ), it works.

echo json_encode(Item::first());

Please or to participate in this conversation.