Create event-driven architecture
✓Works with OpenClaudeYou 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
EventEmitterfrom theeventsmodule (all modern versions do) - Confirm your project has a
package.jsonand decide if you neednpm install eventemitter2for advanced features or use Node's built-inEventEmitter
Steps
- Import the
EventEmitterclass from Node.js's built-ineventsmodule or installeventemitter2for namespaced events and wildcards - Create a singleton event bus by instantiating
EventEmitteronce and exporting it across your application - Define event names as string constants in a separate file to avoid typos and enable refactoring
- Register listeners using
.on(eventName, handler)in modules that need to react to events - Register one-time listeners with
.once(eventName, handler)for events that should trigger only the first time - Emit events from your business logic using
.emit(eventName, ...args)with relevant payload data - Implement error handling via the
errorevent listener to catch unhandled errors in event handlers - 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
Related Backend Skills
Other Claude Code skills in the same category — free to download.
Express Setup
Scaffold Express.js app with best practices
Fastify Setup
Scaffold Fastify app with plugins
NestJS Module
Generate NestJS modules, controllers, services
Middleware Chain
Create and organize middleware chain
Queue Worker
Set up job queue with Bull/BullMQ
File Upload Handler
Create file upload handling with validation
Email Service
Set up transactional email service
WebSocket Setup
Implement WebSocket server with rooms
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.