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.

The example below shows how to use the events module in a function:

/**
* An example of using the Node.js `events` module in Azion Functions.
* Support:
* - Partial support
* - Extended by library `events`
* @example
* // Execute with Azion Bundler:
* npx edge-functions build
* npx edge-functions dev
*/
import { EventEmitter } from "node:events";
/**
* Emit an event and listen to it.
* @param {*} event
* @returns {Response} Response
*/
const main = async (event) => {
const emitter = new EventEmitter();
emitter.on("hello-event", (...args) => {
console.log("an event occurred!", ...args);
});
emitter.emit("hello-event", 1, 2, 3);
return new Response("Event emitted", { status: 200 });
};
export default main;