title: Node.js Readable Stream _read Usage
body: <p>I understand how to use writable streams in Node's new <code>Streams2</code> library, but I don't understand how to use readable streams.</p>
  
  <p>Take, for example, a stream wrapper around the <code>dgram</code> module:</p>
  
  <pre><code>var dgram = require('dgram');
  
  var thumbs = {
    twiddle: function() {}
  };
  
  var defaults = {
    address: '0.0.0.0',
    type: 'udp4',
    port: 12345,
    broadcast: null,
    multicast: null,
    multicastTTL: 1
  };
  
  var UDPStream = function(options) {
    if (!(this instanceof UDPStream))
      return new UDPStream(options);
  
    Duplex.call(this);
  
    options = options || {};
  
    this.address = options.address || defaults.address;
    this.type = options.type || defaults.type;
    this.port = options.port || defaults.port;
    this.broadcast = options.broadcast || defaults.broadcast;
    this.multicast = options.multicast || defaults.multicast;
    this.multicastTTL = options.multicastTTL || defaults.multicastTTL;
  
    this._socket = dgram.createSocket(this.type, setup.bind(this));
    this._socket.on('message', this.push.bind(this));
  };
  
  util.inherits(UDPStream, Duplex);
  
  var setup = function() {
    if (this.multicast) {
      this._socket.addMembership(this.multicast);
      this._socket.setMulticastTTL(this.multicastTTL);
  
      this.destination = this.multicast;
    } else {
      // default to using broadcast if multicast address is not specified.
      this._socket.setBroadcast(true);
  
      // TODO: get the default broadcast address from os.networkInterfaces() (not currently returned)
      this.destination = this.broadcast || '255.255.255.255';
    }
  };
  
  UDPStream.prototype._read = function(size) {
    thumbs.twiddle();
  };
  
  UDPStream.prototype._write = function(chunk, encoding, callback) {
    this._socket.send(chunk, 0, chunk.length, this.port, this.destination);
    callback();
  };
  
  module.exports = UDPStream;
  </code></pre>
  
  <p>Everything makes sense except for the <code>_read</code> implementation. It's literally twiddling thumbs because I don't understand what I'm supposed to do there. My data is pushed when the udp socket emits a new message, but I have no way of pausing or resuming the underlying resource. What <em>should</em> this look like?</p>