Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the ".on" in "process.stdin.on" for?

Tags:

node.js

stdin

I was on the Node.js website and I had some example code from a friend containing "stdin". I went searching what stdin was and I do know now. Although, on the website from Node.js, they use "stdin.on".

I cannot find ANYTHING about it. Maybe someone can fill me in?! :)

process.stdin.setEncoding('utf8');

process.stdin.on('readable', () => {
  var chunk = process.stdin.read();
  if (chunk !== null) {
    process.stdout.write(`data: ${chunk}`);
  }
});

process.stdin.on('end', () => {
  process.stdout.write('end');
});

I was hoping someone could explain this to me on a non-expert level.

like image 409
Kerwin Sneijders Avatar asked Jan 14 '17 22:01

Kerwin Sneijders


People also ask

What is input process Stdin?

stdin property is an inbuilt application programming interface of the process module which listens for the user input. The stdin property of the process object is a Readable Stream. It uses on() function to listen for the event.

How do I stop a process from using Stdin?

`process. stdin. pause` will "close" `stdin`.

What is stdin and stdout in Nodejs?

stdin (0): The standard input stream, which is a source of input for the program. process. stdout (1): The standard output stream, which is a source of output from the program. process. stderr (2): The standard error stream, which is used for error messages and diagnostics issued by the program.


2 Answers

The process.stdin property returns a net.Socket Stream connected to stdin. ^

It extends from stream.Duplex which makes it both readable and writable. ^

All streams are instances of EventEmitter. ^

Now that we understand this, let's look what really is that you can find in process.stdin.

.addListener(eventName, listener) ^

  • alias for .on(eventName, listener)

.on(eventName, listener) ^

  • adds the listener function to the end of the listeners array for the event named eventName

.once(eventName, listener) ^

  • adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

.off(eventName, listener) ^

  • removes the specified listener from the listener array for the event named eventName

.removeListener(eventName, listener) ^

  • alias for .off(eventName, listener)

What this means is that you can attach "listeners" which execute actions in response to various events emitted by stdin.

For example, when user presses a key. It is similar to DOM events such as onClick="myFunction()" attribute, object.onclick or jQuery.on('click').

like image 197
Qwerty Avatar answered Oct 25 '22 01:10

Qwerty


Been struggling with the same question myself recently and after a bit of digging I found that according to the Node.Js documentation:

The process object is an instance of EventEmitter

If you head over to the EventEmitter documentation you can find more about the API and the on functionality there:

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

In my case it was looking through the TypeScript definition file for Node that led me down that route, with the following API methods:

export class EventEmitter {
    addListener(event: string | symbol, listener: Function): this;
    // Here is it
    on(event: string | symbol, listener: Function): this;
    once(event: string | symbol, listener: Function): this;
    removeListener(event: string | symbol, listener: Function): this;
    removeAllListeners(event?: string | symbol): this;
    setMaxListeners(n: number): this;
    getMaxListeners(): number;
    listeners(event: string | symbol): Function[];
    emit(event: string | symbol, ...args: any[]): boolean;
    listenerCount(type: string | symbol): number;
    // Added in Node 6...
    prependListener(event: string | symbol, listener: Function): this;
    prependOnceListener(event: string | symbol, listener: Function): this;
    eventNames(): (string | symbol)[];
}
like image 23
Joseph Woodward Avatar answered Oct 24 '22 23:10

Joseph Woodward