-
Hey I've built my owner socket_streaming_client php client in PHP that can yield back to the parent caller, enabling a parent or initiating function to be arbiter of how the live streaming data is handled.. So I know it's gotta be possible with reactPHP.. at least it should. So I created a client class with the reactPHP methods, which I am calling like this: ///USAGE I thought reactPHP would give me more rigour and options so i tried this, which btw, works using ECHO per reactPHP documentation here: https://reactphp.org/promise-stream/#unwrapreadable
but when i tried to convert it to a generator approach.. like this..
it doesn't work.. so I then went down the path of...
But no - it's doesn't work either... and i've been getting a hand from copilot and gemini, which have come up with all kinds of crazy, adding promises, promiseInterface, etc.. mixing and matching in all kinds of unsuccessful ways. The issue is that I cannot see ANY references to YIELD in the reactPHP documentation or examples, nor can I find any examples of people discussing this online using regular searches.. So am I crazy? is no-one else encountering this? and is reactPHP constrained to simply echo results back directly and unable to bubble up a live streaming response via yield to the calling / parent function? Please help |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @gidzr, welcome to @reactphp! This sounds like a wild ride, so let me try to clear things up a bit: I'm not sure what you're trying to achieve exactly and without having a better understanding of the motivation, it sounds like you want to turn a ReactPHP is all about event-driven programming, so your program flow would be controlled by events happening as time passes by and consequently allows you to run any concurrent routines asynchronously. In your case, it sounds like you want to have more control over this flow and may want to "wait" (or "block") between receiving any data events. This could probably be done with the following (untested!) snippet: use React\Stream\ReadableStreamInterface;
use function React\Async\await;
use function React\Promise\Stream\first;
function gen(ReadableStreamInterface $stream): Generator
{
while ($stream->isReadable()) {
// simplistic approach to await data
// doesn't take buffering or close/error events into account
yield await(first($stream, 'data'));
}
}
foreach (gen($stream) as $data) {
var_dump($data);
} Note this snippet doesn't take buffering or close/error events into account to keep things simple, but I hope it can be helpful as a starting point. Anything beyond this would probably need a much better understanding of what you're trying to achieve exactly (and why) and might be out of scope here. On top of this, there are a number of oversights in your snippet, so it might also make sense to address these: //setup
- $loop = Factory::create();
- $connector = new Connector($loop);
- $browser = new Browser($loop, $connector)->requestStreaming('POST', $url, $headers, $data)
+ $browser = new Browser();
+ $promise = $browser->requestStreaming('POST', $url, $headers, $data); The
I can see that this looks tempting, however the Stream documentation explicitly mentions that return values from an event callback should be avoided and have no effect (https://github.com/reactphp/stream#readablestreaminterface). This invalid usage would be much easier to detect with PHPStan in ReactPHP v3 (reactphp/stream#180). I hope this helps! 👍 If so, consider supporting this project, for example by becoming a sponsor ❤️ |
Beta Was this translation helpful? Give feedback.
Hey @gidzr, welcome to @reactphp! This sounds like a wild ride, so let me try to clear things up a bit:
I'm not sure what you're trying to achieve exactly and without having a better understanding of the motivation, it sounds like you want to turn a
ReadableStreaminterface
into aGenerator
. This is very much possible, but does require some work because you'd have to take the thing into account that is so obvious we often forget about it: time.ReactPHP is all about event-driven programming, so your program flow would be controlled by events happening as time passes by and consequently allows you to run any concurrent routines asynchronously. In your case, it sounds like you want to have m…