Node.js provides a built-in events module.

It contains an event emitter class that lets you create and handle custom events via callback functions.

Emitting Events

The events module is a core part ofthe Node.js server-side environment.

a person holding up a node.js sticker

Like so:

you’re able to emit events using theEventEmittersemitmethod.

Theemitmethod takes aneventNameand an arbitrary number of arguments as parameters.

Once you call theemitmethod, it emits the passedeventName.

Finally, it returnstrueif the event had listeners andfalseif it didn’t have any listeners.

When the code block above runs it will notify all listeners listening for theTestEventevent.

It will call those listeners with the given arguments.

Listening for Events

you’ve got the option to listen for emitted events using theEventEmittersonmethod.

Theonmethod takes anEventNameand a callback function as parameters.

When the event with theEventNamepassed into theonmethod is emitted, it invokes its callback function.

This method returns a reference to theEventEmitter, allowing you to chain multiple calls.

you might change this behavior using theEventEmittersprependListenermethod.

This method takes the same parameters as theonmethod.

The difference is that this method reacts to the event first, regardless of the time you register it.

irrespective of the order you registered them in because of theprependListenermethod.

If you register several listeners with theprependListenermethod, they will run in order from the last to the first.

Note the arrangement of the emitter and listeners.

The listeners always come before the emitter.

This arrangement is because the listeners must already be listening for the event before the emitter emits it.

it’s possible for you to achieve this using theEventEmittersoncemethod.

This method takes the same arguments as theonmethod and works similarly.

The only difference is that the listeners registered with theoncemethod only listen for the event once.

Listeners registered with theoncemethod react to the event in the order you register them.

you could change this behavior using theprependOnceListenermethod, which works likeprependListener.

The only difference is that the listeners registered with theoncemethod only listen for the event once.

Unhandled errors from them will cause the Node.js process to exit and your app to crash.

To handle an error event, at least one of the events listeners must have itsEventNameset toerror.

Managing Event Listeners

TheEventEmitterclass has several methods that allow you to manipulate and manage event listeners.

removeAllListeners

Removes all listeners for a specified eventName.

If you dont specify an event name, this method call will remove all listeners for the EventEmitter.

setMaxListeners

number

Changes the default max number of listeners per event.

Use infinity or zero to indicate an unlimited number of listeners.

you’ve got the option to only call these methods on anEventEmitterinstance.

The code block above removes a single listener for theTestEventevent.

Event-driven programming is one of the reasons Node.js programs are faster and more straightforward than some alternatives.

you could easily synchronize multiple events, resulting in improved efficiency.