StreamedJsonResponse not streaming -> alternative solution #55509
-
Feature implemented in #49873 (comment) @pelmered In symfony documentation they mention that you should use flush once in a while. We did flush after each 100 rows and also we put a sleep after flushing, but no luck. Analog with ob_flush(). We tested this on our demo page/website. Could there be a server setup that prevents streaming or is this an issue from symfony? Update We set in php.ini
and then the response changed to {
"has_more_pages": false,
"sums": {
"value": null
},
"avgs": {
"value_avg": null
},
"mins": {
"value_min": null,
"created_at_min": null
},
"maxs": {
"value_max": null,
"created_at_max": null
},
"index_required_on_filtering": [
"id",
"parent_id",
"client_id",
"created_at",
"currency",
"value"
],
"current_page": 1,
"from": 1,
"last_page": 1,
"per_page": 3748918,
"to": 3748918,
"total": 3748918,
"data": Previously it was valid json but data did not contain any rows, just a "message": "Server error" |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 4 replies
-
Locally it behaves the same. No streaming involved. index.php: $response = $kernel->handle(
$request = Request::capture()
)->send();
The StreamedResponse seems to be flushing the data after each echo. We tried with cUrl in parallel terminals. In one we tailed the logs while in the other we ran the call. For StreamedResponse both terminals were showing new data async. For StreamedJsonResponse first the yield happened in logs and when that finished then it started sending the data, so in a sync way. For csv download (StreamedResponse) we did not use generator (yield) and no flush or ob_flush. Locally we use: PHP 8.2.17 (cli) (built: Mar 16 2024 01:08:19) (NTS) The only difference between the 2 situations seems to be LazyCollection (json) vs EloquentCollection (csv). |
Beta Was this translation helpful? Give feedback.
-
Acc to this jsons can't be streamed while using json_encode, which is used in this case. |
Beta Was this translation helpful? Give feedback.
-
Yeah, we had some problems with this as well. We didn't dig much deeper into this than you have it looks like and we ended up abandoning this. Our plan now is to switch to Event Streams instead, but we haven't tried that yet. That looks to be pretty straight forward to handle in the front end as well by the looks of it: https://developer.mozilla.org/en-US/docs/Web/API/EventSource |
Beta Was this translation helpful? Give feedback.
-
Calling after each yield + echo for 20000 rows: |
Beta Was this translation helpful? Give feedback.
-
php class with JS example code for handling the new streamed json |
Beta Was this translation helpful? Give feedback.
-
@pelmered This will not open a new tab with prompt for download the json but it will print it in html. We will keep the demo on laravel 10 with the not streaming version of StreamedJsonResponse for a while to see the differences. We published this in laravel-crud-wizard-free v 3.5.0 https://github.com/macropay-solutions/laravel-crud-wizard-free/releases/tag/3.5.0 but NOT in its demo pages. |
Beta Was this translation helpful? Give feedback.
-
Update for symfony. We gave them a demo project to reproduce the issue. symfony/symfony#60257 (comment) |
Beta Was this translation helpful? Give feedback.
@pelmered
php class with JS example code for handling the new streamed json
https://github.com/macropay-solutions/laravel-crud-wizard-free/blob/production/src/Responses/StreamedJsonResponse.php