Home

EventEmitter

EventEmitter is a class in Node.js' events module that provides a lightweight publish/subscribe pattern for event-driven programming. It enables objects to emit named events and allows other code to register listeners that respond when those events occur. This pattern is foundational to many Node.js core modules and user-defined components.

The core API centers on methods for managing listeners and emitting events. The on or addListener methods

Behavior and usage patterns: listeners are invoked in the order they were added. If a listener throws

Subclassing and usage: developers often subclass EventEmitter to create custom components that expose an event-based API.

register
a
listener
function
for
a
given
event
name.
The
once
method
registers
a
listener
that
runs
only
the
first
time
the
event
is
emitted.
The
emit
method
triggers
all
listeners
registered
for
a
given
event
name,
passing
along
any
arguments
to
the
listeners.
Listeners
can
be
removed
with
removeListener
or
removeAllListeners,
and
the
current
set
of
listeners
for
an
event
can
be
retrieved
with
listeners.
The
setMaxListeners
method
can
be
used
to
adjust
the
reported
limit
on
how
many
listeners
can
be
attached
to
a
single
event,
helping
to
detect
potential
memory
leaks.
an
error,
it
may
affect
subsequent
listeners
unless
handled.
EventEmitter
also
supports
querying
the
number
of
listeners
indirectly,
and
it
provides
a
mechanism
to
warn
about
possible
memory
leaks
when
many
listeners
are
attached
to
the
same
event.
It
is
a
core
module
in
Node.js
and
is
not
part
of
standard
browser
APIs,
where
EventTarget
or
other
libraries
provide
similar
functionality.
EventEmitter
remains
a
central
tool
for
coordinating
asynchronous
activities
in
server-side
JavaScript
environments.