node:events
The node:events module in Azion Runtime: the EventEmitter class, examples of listeners, async handlers, and subclasses, and the supported APIs.
The node:events module provides EventEmitter, the Node.js class for event-driven code. An event emitter is an object that emits named events, and listeners registered on it run each time it emits one of those names. In Azion Runtime, functions use it to decouple logic: one part of the code emits an event, and dedicated listeners react to it.
Examples
Each example is a complete function that imports EventEmitter from node:events. The response below each example is the one a deployed function returns.
Basic event emission
This function registers a listener for one event, then emits that event with three arguments:
The listener logs an event occurred! 1 2 3, and the function responds with:
Request lifecycle events
This function emits one event per processing stage of the request and records what each listener saw:
For a request to https://example.com/, the function responds with the three stages in the order it emitted them:
One-time listeners and error handling
This function registers a listener that runs once, two listeners for the same event, and a listener for the error event:
The function responds with one Initialized entry, both listeners’ entries for each chunk, and the error:
Async event handlers
This function emits an event from each of two parallel fetch calls and waits for both calls with Promise.all(). emit() does not wait for a Promise that a listener returns, so the function awaits the fetches themselves:
The function responds with the results the listener collected and the results Promise.all() returned:
Custom EventEmitter class
This function extends EventEmitter into a class for one domain, processes two requests with it, and counts the listeners of one event:
For a request to https://example.com/, the function responds with the log of both requests and the request count:
Supported APIs
The table lists the status of each node:events API in Azion Runtime:
| API | Status |
|---|---|
new EventEmitter() | 🟢 Supported |
emitter.on() | 🟢 Supported |
emitter.once() | 🟢 Supported |
emitter.emit() | 🟢 Supported |
emitter.off() | 🟢 Supported |
emitter.removeListener() | 🟢 Supported |
emitter.removeAllListeners() | 🟢 Supported |
emitter.listenerCount() | 🟢 Supported |
emitter.listeners() | 🟢 Supported |
emitter.eventNames() | 🟢 Supported |
emitter.prependListener() | 🟢 Supported |
emitter.prependOnceListener() | 🟢 Supported |
emitter.setMaxListeners() | 🟢 Supported |
emitter.getMaxListeners() | 🟢 Supported |
EventEmitter.listenerCount() | 🟢 Supported |
EventEmitter.defaultMaxListeners | 🟢 Supported |
events.once() | 🟢 Supported |
captureRejections | 🟡 Partially supported |
APIs marked 🟡 Partially supported have limited functionality compared to the full Node.js implementation. The captureRejections option is one of them, so handle Promise rejections explicitly in async listeners instead of relying on automatic capture.
In Azion Runtime, the module-level events.once() function returns a Promise that resolves with an array of the arguments the event carried. An error event emitted with no error listener throws the emitted error. When an event has more listeners than the limit set with emitter.setMaxListeners(), the runtime logs this warning: