The behaviour of standard input's readable event in Node JS

40 views Asked by At

I wrote a UDP client to send lines from standard input to an UDP socket:

var PORT = 12000;
var HOST = '127.0.0.1';

var dgram = require('dgram');

var client = dgram.createSocket('udp4');

process.stdin.on("readable",
  function() {
    var chunk = process.stdin.read();
    if (chunk !== null) {
      client.send(chunk, PORT, HOST);
    }
  }
);

client.on("message",
  function (message, remote) {
    process.stdout.write(message);
  }
);

Now, the readable event fires on the first time but stops working afterwards.

I successfully used this on a TCP chat client and server before: I got a readable event infinitely.

What could cause the problem here?

1

There are 1 answers

0
Gergely On

The code works if I subscribe to the data event on standard input. That fires every time I type a new line into the standard input.

See data event documentation at Stream class.