Node Event Emitters — For Beginners and Experts
For now, we’ll only pay attention to these two member functions:

on(eventName, …)

emit(eventName, …)

To publish an event, we use the emit() function, and to listen to an event, we use the on() function.

In EventEmitters , we publish and listen to the events by name.

In the last code snippet, where we created an EventEmitter object, we use the following code to raise events and listen to them.

Running the above code snippet prints the following output in the console:

> Data Received

In the above code snippet, we raised an event with the name myEvent on the last line, and we had a listener registered to the event just above the line of the publishing event.

At the time of publishing the event, there must be an EventEmitter listener existing to listen to the published event.

For example, if we change the above code snippet to …

… the output will be:

> Listener 1
['Kunal Tandon']
['Programming', 'Technology', 'JavaScript', 'Software Development', 'Nodejs']