0

TCP stream can be piped, e.g. below:

 const net = require("net"),                                                                                           
     port = 8888;                                                                                                      

 net.createServer(conn => conn.pipe(conn)).listen(port);                                                               

 const sock = net                                                                                                      
     .createConnection(port, () => sock.write("hello"))                                                                
     .on("data", data => console.log("echo: " + data));       

But why doesn't nodejs support stream pipe on UDP at all? e.g. below:

const dgram = require("dgram"),                                                                                       
     port = 8888;                                                                                                      

sock = dgram.createSocket("udp4", (data, rinfo) => {                                                                  
    // sock.pipe(sock); /* pipe not implemented */                                                                              
    sock.send(data, rinfo.port); /* this is ok instead. */                                                                              
});                                                                                                                   
sock.bind(port);                                                                                                      

dgram                                                                                                                 
    .createSocket("udp4", data => {                                                                                   
        console.log("echo: " + data);                                                                             
        process.exit();                                                                                               
    })                                                                                                                
    .send("hello", port);                                                           
sof
  • 9,113
  • 16
  • 57
  • 83
  • **But why doesn't nodejs support stream pipe on UDP at all?**. These types of questions are generally not useful here. It's not a programming question at all. It's a question about history in the internals of some team. It would require someone who was involved in the design and implementation of streams who could explain whether this was even considered and, if so, why they decided not to implement it. – jfriend00 Apr 11 '20 at 17:47
  • I can offer that a stream readable or a stream writable is a generic framework for any transport. You can subclass and implement a few methods and have your own udp-based stream if you want. – jfriend00 Apr 11 '20 at 17:47
  • UDP does not deal in streams. It deals in discrete messages (datagrams) which can be lost, duplicated or reordered in transit, and messages from multiple sources will be interleaved on a receiving socket. You could build some sort of "lossy message stream" abstraction on a UDP socket but that's not the kind of stream that the `.pipe` method wants to work with. – ottomeister Apr 12 '20 at 02:30

0 Answers0