Data event is not emitted.
g105b opened this issue ยท 5 comments
Hi, please can you let me know if there is something wrong with how I am using the library?
Example code:
<?php
require("vendor/autoload.php");
$loop = React\EventLoop\Factory::create();
$stream = new React\Stream\Stream(fopen("php://memory", "w+"), $loop);
$stream->on("data", function($data) {
echo "Got data: $data" . PHP_EOL;
});
$loop->addPeriodicTimer(1, function() use($stream) {
echo "Tick." . PHP_EOL;
$stream->write("Hello, world!");
});
$loop->run();
Output:
php sandbox.php
Tick.
Tick.
Tick.
Tick.
Tick.
Tick.
Tick.
Tick.
...
Notice there is no "Got data" being displayed, ever.
is something wrong with how I am using the library?
Hi and welcome to React! :-) So what are you trying to achieve? :-)
Using a php://memory
stream with React is really only useful for tests afaict.
The data
event will only be emitted once new data arrives, think of incoming data from a duplex TCP/IP connection.
The write()
method will only write to your socket resource, think of writing data to your TCP/IP connection.
This means that the data you read is not necessarily correlated with the data you write.
I hope this helps ๐
Thanks for the quick response. I'll give you some context of my project and what I'm trying to achieve, hopefully you can help me understand the library better.
Currently, I have a function that is provided chunks of data as the data comes in. It is polled from within a React Loop. It works nicely so far, but I need a way of streaming the data rather than writing it to file or a variable.
The trouble is, the data can be potentially quite massive, and multiple data sources can be read concurrently. I don't want to store the whole data and then process it after it has completed, I want to be able to put it into what I'm calling a "Stream", so that another function within the loop can be alerted when the stream receives data.
I hope this makes sense. Is my use case intended for this library or have I got the wrong end of the stick?
P.S. If I were to use a file instead of php://memory
i.e. fopen("/tmp/test.txt", "w+")
, there is no change to the output - still tick tick tick tick tick.
I'm not sure I follow what you're trying to achieve, so I guess a simple gist might help here :-)
From what I understand it sounds like your may need a function that returns an instance implementing ReadableStreamInterface
(possibly just ReadableStream
) which emits your data and then pipe()
this stream into your destination stream (implementing WritableStreamInterface
).
It's a bit unclear where the "data" comes from, so obviously the data will need to be emitted by your source stream (read from a stream socket?).
I hope this helps ๐
I believe this has been answered, so I'm closing this for now. Please come back with more details if this problem persists and we can reopen this ๐
Thanks, put this down to my misunderstanding and not a bug in the code.