LLaraNode
Events Package

Event Listeners

Listeners handle events when they are dispatched.

Event Listeners

Listeners handle events when they are dispatched.

Creating Listeners

import { ListensTo } from "@lara-node/events";

@ListensTo(UserRegistered)
class SendWelcomeEmail {
  async handle(event: UserRegistered) {
    await Mail.to(event.user.email).send(new WelcomeMail(event.user));
  }
}

Multiple Listeners

Multiple listeners can listen to the same event:

@ListensTo(UserRegistered)
class SendWelcomeEmail {
  async handle(event: UserRegistered) {
    // Send email
  }
}

@ListensTo(UserRegistered)
class NotifyAdmin {
  async handle(event: UserRegistered) {
    // Notify admin
  }
}

Listening to Multiple Events

@ListensTo(UserRegistered)
@ListensTo(UserUpdated)
class UpdateSearchIndex {
  async handle(event: UserRegistered | UserUpdated) {
    // Update search index
  }
}

Registering Listeners Manually

import { on, event } from "@lara-node/events";

on(UserRegistered, async (event) => {
  console.log("User registered:", event.user.email);
});

// Dispatch
await event(new UserRegistered(user));

Once Listeners

import { once } from "@lara-node/events";

once(AppStarted, async () => {
  console.log("This runs only once");
});

Removing Listeners

import { off } from "@lara-node/events";

off(UserRegistered, listenerFunction);

Event Naming

Customize event names:

import { EventName } from "@lara-node/events";

@EventName("user.registered")
class UserRegistered {
  constructor(public user: User) {}
}

Next Steps