Default Events
Services automatically emit the following events:- created - Emitted after a resource is created
- updated - Emitted after a resource is updated
- patched - Emitted after a resource is patched
- removed - Emitted after a resource is removed
Event Listeners
Services extend Node’sEventEmitter, so you can use all standard EventEmitter methods:
on()
Listen for an event:string
required
The event name (e.g.,
'created', 'patched')function
required
Callback function that receives the event data and optional hook context
once()
Listen for an event once:removeListener()
Remove a specific event listener:removeAllListeners()
Remove all listeners for an event or all events:Event Emission
Events are automatically emitted after successful service method calls unless:- The service’s
eventsoption includes the event name (indicating the service handles emission itself) - The
context.eventproperty is set tonullin a hook
Automatic Event Emission
Controlling Event Emission
You can control event emission in hooks:Custom Events
You can define custom events when registering a service:string[]
List of custom event names. These are merged with the default events (
created, updated, patched, removed).Service Events Configuration
You can completely replace the default events:string[]
Complete list of events for this service. Unlike
events, this replaces the default events entirely.Event Data
Events receive two arguments:- data - The resource that was created, updated, patched, or removed
- context - The hook context (optional)
Multiple Results
When a method returns multiple results (e.g.,create with an array), the event is emitted once for each result:
Application-Level Events
The application itself is an EventEmitter and can be used for custom application events:Real-Time Transports
Events are the foundation for real-time updates in Feathers. When using transports like Socket.io or Primus, service events are automatically sent to connected clients.Server-Side
Client-Side
Event Filtering
For real-time applications, you often want to filter which clients receive which events. This is done through channels (see Publishing & Channels documentation).Event Hook
Feathers uses an internaleventHook to handle event emission: