Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Backendintermediate

Event Emitter Setup

Share

Create event-driven architecture

Works with OpenClaude

You are a backend architect specializing in event-driven systems. The user wants to create a robust event emitter setup for decoupled, scalable backend services.

What to check first

  • Verify Node.js version supports EventEmitter from the events module (all modern versions do)
  • Confirm your project has a package.json and decide if you need npm install eventemitter2 for advanced features or use Node's built-in EventEmitter

Steps

  1. Import the EventEmitter class from Node.js's built-in events module or install eventemitter2 for namespaced events and wildcards
  2. Create a singleton event bus by instantiating EventEmitter once and exporting it across your application
  3. Define event names as string constants in a separate file to avoid typos and enable refactoring
  4. Register listeners using .on(eventName, handler) in modules that need to react to events
  5. Register one-time listeners with .once(eventName, handler) for events that should trigger only the first time
  6. Emit events from your business logic using .emit(eventName, ...args) with relevant payload data
  7. Implement error handling via the error event listener to catch unhandled errors in event handlers
  8. Use .removeListener() or .off() when cleaning up subscriptions in destructors or cleanup functions

Code

// eventBus.js - Central event emitter singleton
const EventEmitter = require('events');

class EventBus extends EventEmitter {
  constructor() {
    super();
    this.setMaxListeners(20); // Prevent memory leak warnings
  }
}

module.exports = new EventBus();

// ---

// events.js - Event name constants
const EVENTS = {
  USER_CREATED: 'user:created',
  USER_UPDATED: 'user:updated',
  USER_DELETED: 'user:deleted',
  ORDER_PLACED: 'order:placed',
  PAYMENT_PROCESSED: 'payment:processed',
  ERROR: 'error'
};

module.exports = EVENTS;

// ---

// userService.js - Emit events
const eventBus = require('./eventBus');
const EVENTS = require('./events');

async function createUser(userData) {
  const user = { id: 123, ...userData, createdAt: new Date() };
  
  // Business logic
  await saveToDatabase(user);
  
  // Emit event for other services
  eventBus.emit(EVENTS.USER_CREATED, user);
  
  return user;
}

async function updateUser(userId, updates) {
  const user = await getUser(userId);
  Object.assign(user, updates);
  await saveToDatabase(user);
  
  eventBus.emit(EVENTS.USER_UPDATED, user);
  return user;
}

module.exports = { createUser, updateUser };

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

CategoryBackend
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
backendeventsarchitecture

Install command:

curl -o ~/.claude/skills/event-emitter-setup.md https://claude-skills-hub.vercel.app/skills/backend/event-emitter-setup.md

Related Backend Skills

Other Claude Code skills in the same category — free to download.

Want a Backend skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.