Node.js API compatibility - Events
Explore the integration and application of the Node.js Events API within the Azion Runtime. The Events module is a fundamental part of Node.js, providing the backbone for handling asynchronous events through the implementation of the EventEmitter class.
The events API in Node.js is a core module that provides a way to work with event-driven programming. It allows developers to create and manage event emitters, which are objects that can emit named events and listen for those events through event listeners. This module is available in the Azion Runtime through Node.js compatibility, making it useful for decoupling logic in functions by emitting events and reacting to them with dedicated listeners.
Example: Basic event emission
The example below shows how to use the events module in a function:
Example: Request lifecycle events
Use EventEmitter to handle request processing stages:
Example: One-time listeners and error handling
Use once() for single-execution listeners and handle errors properly:
Example: Async event handlers
Handle asynchronous operations in event listeners:
Example: Custom EventEmitter class
Extend EventEmitter to create domain-specific event emitters:
Supported APIs
| 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 |
EventEmitter.listenerCount() | 🟢 Supported |
EventEmitter.defaultMaxListeners | 🟢 Supported |
captureRejections | 🟡 Partially supported |