Node event emitter

From Intense-Visions/harness-engineering · View interactive version

Node event emitter is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Use Node.js EventEmitter for typed pub-sub communication with memory leak prevention

How to use this skill

Skill file

Node.js EventEmitter

Use Node.js EventEmitter for typed pub-sub communication with memory leak prevention

When to Use

Instructions

  1. Basic EventEmitter usage:
import { EventEmitter } from 'node:events';

const emitter = new EventEmitter();

emitter.on('user:created', (user: { id: string; name: string }) => {
  console.log(`User created: ${user.name}`);
});

emitter.emit('user:created', { id: '1', name: 'Alice' });
  1. Typed EventEmitter in TypeScript:
import { EventEmitter } from 'node:events';

interface AppEvents {
  'user:created': [user: { id: string; name: string }];
  'user:deleted': [userId: string];
  error: [error: Error];
}

class AppEmitter extends EventEmitter<AppEvents> {}

const emitter = new AppEmitter();

emitter.on('user:created', (user) => {
  // user is typed as { id: string; name: string }
  console.log(user.name);
});
  1. One-time listeners with once:
emitter.once('ready', () => {
  console.log('System initialized');
});
  1. Remove listeners to prevent memory leaks:
const handler = (data: string) => console.log(data);
emitter.on('data', handler);

// Later: remove the specific listener
emitter.off('data', handler);

// Or remove all listeners for an event
emitter.removeAllListeners('data');
  1. Set max listeners to catch leaks early:
emitter.setMaxListeners(20); // Default is 10

// Or globally
EventEmitter.defaultMaxListeners = 20;
  1. Async event handling with once as a Promise:
import { once } from 'node:events';

const [data] = await once(emitter, 'data');
console.log(data);
  1. Error handling — always listen for 'error' events:
emitter.on('error', (err) => {
  console.error('Emitter error:', err);
});

// Without an error listener, emitting 'error' throws and crashes the process
  1. AbortController for cleanup:
const ac = new AbortController();

emitter.on('data', handler, { signal: ac.signal });

// Automatically removes the listener
ac.abort();

Details

EventEmitter is Node.js's built-in pub-sub mechanism. It is synchronous by default — emit() calls listeners in registration order and blocks until all complete.

Synchronous emission: emitter.emit('event', data) runs all listeners synchronously in the current tick. Long-running listeners block the event loop. Use setImmediate or queueMicrotask inside listeners for async work.

Memory leak warning: If more than maxListeners are registered for a single event, Node.js prints a warning. This usually indicates listeners being added in a loop without removal.

Event ordering: Listeners fire in registration order. prependListener adds to the front of the queue.

Trade-offs:

Source

https://nodejs.org/api/events.html

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.