How Events Work
Every service is an EventEmitter and automatically emits events for data-modifying methods:service.ts:17-24
- Created
- Updated
- Patched
- Removed
Listening to Events
Service Events
Listen to events on a specific service:Event Callback Parameters
Event listeners receive two parameters:events.ts:18
Multiple Records
When operations affect multiple records, events are emitted for each:events.ts:16-19
Custom Events
Define custom events for your service:service.ts:45-47
Custom events defined in the
events option are merged with default events (created, updated, patched, removed).Controlling Event Emission
Set Event in Hook Context
Control which event is emitted using thecontext.event property:
events.ts:6-21
Disable Events
Setcontext.event = null to prevent event emission:
Service Events Configuration
Customize the complete list of events a service emits:Real-Time with Transports
Events are automatically sent to connected clients via transports:Event Filtering & Publishing
Control which clients receive events using publishers:Event Patterns
Notifications
Audit Logging
Cache Invalidation
Webhook Triggers
Data Synchronization
Events vs Hooks
- When to Use Events
- When to Use Hooks
Use events when:
- You need to react to changes after they happen
- Multiple independent systems need to know about changes
- You’re building real-time features
- You need loose coupling between services
- You want to broadcast to multiple clients
Event Mixin
Feathers automatically adds EventEmitter functionality to services:events.ts:23-31
Best Practices
- Keep event handlers lightweight - Don’t block the event loop
- Handle errors in listeners - Wrap async operations in try/catch
- Avoid circular dependencies - Don’t call service methods that emit the same event
- Use custom events for domain events - Make events meaningful
- Clean up listeners - Remove listeners when components unmount
- Test event handlers - Unit test event logic separately
- Document custom events - Make it clear what events services emit
Debugging Events
Enable debug logging for events:Next Steps
Errors
Learn about error handling in Feathers
Real-time Transport
Control event distribution with Socket.io channels