I am still not clear on how the data flows between stream in pipe. Most examples are using file or built in generator as starting point, then finished in another file or output. But most of them didn't show anything about how to make our own pipeline.
So, I need to generate data I received from another data from an external machine, then transform it into something, and retransmit the data via UDP socket (no stream-udp
package please). I know how to get the data, and I know how to send data via datagram, but I am confused on how you can stream them as the data received and sent.
Let's say. I have a something like this:
import { pipeline, Readable, Transform, Writable } from "stream";
const reader = new Readable();
const transform = new Transform();
const writable = new Writable();
// How to flow the data from reader to transform to writer?
transform.on('data', (chunk) => {
console.log('Transforming ' + chunk);
const newchunk = chunk + 'transformed ';
// how to send the data from here to writable?
transform.push(newchunk);
})
writable.on('???', (chunk) => {
// write the chunk into something
console.log(chunk);
}
pipeline(
reader,
transform,
writable,
(err) => {
console.log('Done')
if (err) console.error(err);
}
)
reader.push('this is new data');
So, can someone please help me on how the things flow from the reader sending a text to transform, then transform, then received at writable?
A simple example should suffice, like reader.push('this is data')
and ends with writer.on('???', (chunk) => console.log(chunk))
Thank you.