Sails app instances inherit Node's EventEmitter
interface, meaning that they can both emit and listen for custom events. While it is not recommended that you utilize Sails events directly in app code (since your apps should strive to be as stateless as possible to facilitate scalability), events can be very useful when extending Sails (via hooks or adapters) and in a testing environment.
Most Sails developers never have a use case for working with application events. Events emitted by the Sails app instance are designed to be used when building your own custom hooks, and while you could technically use them anywhere, in most cases you should not. Never use events in your controllers, models, services, configuration, or anywhere else in the userland code in your Sails app (unless you are building a custom app-level hook in api/hooks/
).
The following are the most commonly used built-in events emitted by Sails instances. Like any EventEmitter in Node, you can listen for these events with sails.on()
:
sails.on(eventName, eventHandlerFn);
None of the events are emitted with extra information, so your eventHandlerFn
should not have any arguments.
Event name | Emitted when... |
---|---|
ready |
The app has been loaded and the bootstrap has run, but it is not yet listening for requests |
lifted |
The app has been lifted and is listening for requests. |
lower |
The app has is lowering and will stop listening for requests. |
hook:<hook identity>:loaded |
The hook with the specified identity loaded and ran its initialize() method successfully. |
In addition to
.on()
, Sails also exposes a useful helper function calledsails.after()
. See the inline documentation in Sails core for more information.