Event.on('new::user', async (user) => {
})
// OR
Event.on('new::user', 'User.registered')
You are viewing the legacy version of AdonisJS. Visit https://adonisjs.com for newer docs. This version will receive security patches until the end of 2021.
AdonisJs ships with a dedicated Event provider, which internally uses EventEmitter2 with a bunch of convivence on top of it.
Also, the event provider has a fake implementation, which can be used for assertions when writing tests.
Event listeners are defined inside start/events.js
file.
Events listeners can be defined as Closures or you can bind an IoC container namespace.
Event.on('new::user', async (user) => {
})
// OR
Event.on('new::user', 'User.registered')
The app/Listeners
directory is dedicated for storing Event listeners.
When binding listeners to the events, you are not required to enter the entire namespace. For example, A listener stored as app/Listeners/User.js
is referenced as User.<method>
.
Make use of make:listener
command to create a new event listener.
adonis make:listener User
✔ create app/Listeners/User.js
Let’s say we want to emit an event every time a user register on our website, and inside the event listener, we send an email to the registered user.
Route.post('register', 'UserController.register')
const Event = use('Event')
class UserController {
register () {
// register the user
Event.fire('new::user', user)
}
}
Next, we need to bind a listener for the new::user
event, so that we can send the email. The same is done by creating a file inside the start
directory.
# Mac / Linux
touch start/events.js
# Windows
type NUL > start/events.js
Now let’s write the following code inside it.
const Event = use('Event')
const Mail = use('Mail')
Event.on('new::user', async (user) => {
await Mail.send('new.user', user, (message) => {
message.to(user.email)
message.from('from@email')
})
})
As you can see, AdonisJs makes it easier to use the await
keyword inside the Event listener callback.
Below is the list of methods that can be used to interact with the Event provider.
Bind a single or multiple listeners for a given event. The listener
can be a function or reference to the IoC container binding.
Event.on('new::user', async () => {
})
// IoC container binding
Event.on('new::user', 'User.registered')
// Array of listeners
Event.on('new::user', ['Mailer.sendEmail', 'SalesForce.trackLead'])
The when
method is an alias of on
method.
Same as on, but instead called only for one time.
Event.once('new::user', () => {
console.log('executed once')
})
Bind listener for any event.
Event.onAny(function () {
})
// Ioc container binding
Event.onAny('EventsLogger.track')
The times
is chained with on
or when
to limit the number of times the listener should be fired.
Event
.times(3)
.on('new::user', () => {
console.log('fired 3 times')
})
Emit an event with optional data. Also, you can use fire
method, which is an alias for emit
.
Event.emit('new::user', user)
Remove listener(s) for a given event.
You must bind an IoC container reference to remove it later. |
Event.on('new::user', 'User.registered')
// later remove it
Event.removeListener('new::user', 'User.registered')
Also, you can make use of the alias method called off
.
Returns the number of listeners for a given event.
Event.listenersCount('new::user')
Returns an array of listeners for a given event.
Event.getListeners('new::user')
Find if there are any listeners for a given event.
Event.hasListeners('new::user')