Ecommerce Shopify WordPress Discussion

Using StreamedResponse for a rest api endpoint in WordPress

I'm trying to do something like the following where I have a rest api endpoint - and I'd like to return a streamed response. However, when I try this in Wordpress it seems to get an error on the new StreamedResponse() call. Best I can tell with the debugger it never seems to call the callback function I have setup. Has anyone done this in a custom Wordpress plugin and got it working? Thanks. use Symfony\Component\HttpFoundation\StreamedResponse; ... function bootstrap(): void { add_action( 'rest_api_init', __NAMESPACE__ . '\\register_chatbot' ); ... function register_chatbot(): void { // Add route for POST to 'gsb-chatbot/v1/conversation' endpoint. register_rest_route( 'gsb-chatbot/v1', '/conversation', array( 'methods' => 'POST', 'callback' => __NAMESPACE__ . '\\gsb_conversation_callback', 'permission_callback' => '__return_true', ) ); } function gsb_conversation_callback($request) { ... return new StreamedResponse(function () use ($stream) { $fullText = ''; foreach ($stream as $response) { if ($response->choices[0] != NULL) { ... $json_string = json_encode($response_obj); echo $json_string . "\n"; ob_flush(); flush(); } } }, 200, [ 'Content-Type' => 'text/event-stream', ]);
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.