Stream receiving messages with stream=true in PHP

Stream receiving messages with stream=true in PHP

Hi all. There is a RASA server and a PHP website where a custom widget in JS is located. Everything works perfectly.

The RASA documentation states that the RASA service supports streaming messaging. In the request to the RASA API that we are making, we added the stream=true parameter, then the responses should not arrive in a batch at once, but one after another as they arrive. For the user, this will mean that the bot responds instantly.

To implement this solution, we connected ReactPHP. Nothing has changed. Responses come when the request is completed completely. Tell me, what am I doing wrong? Please share your thoughts or solutions

I am publishing my solution (it is not working)

$client = new Browser();
$start = microtime(true);

$client->requestStreaming(
    'POST',
    'https://192.168.0.25/webhooks/rest/webhook?stream=true',
    [
        'Content-Type' => 'application/json'
    ],
    json_encode([
    'sender' => 'test-rg-07',
    'token' => '1239rogn238hHSx',
    'message' => 'Hello, CJ. Let's go to Los Santos',
])
)->then(function (ResponseInterface $response) use ($start) {
    $body = $response->getBody();
    assert($body instanceof Psr\Http\Message\StreamInterface);
    assert($body instanceof React\Stream\ReadableStreamInterface);

    $body->on('data', function ($chunk) use ($start) {
        echo $chunk;
        $diff = sprintf( '%.6f sec.', microtime( true ) - $start);
        echo "Execution time: $diff"; // Execution time: 00.00015
    });

    $body->on('error', function (Exception $e) {
        echo 'Error: ' . $e->getMessage() . PHP_EOL;
    });

    $body->on('close', function () {
        echo '[DONE]' . PHP_EOL;
    });
}, function (Exception $e) {
    echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

#PHP Rasa Open Source #STREAM

I’m not familiar with a stream option for the REST endpoint but you might want to try the socket channel.