createEventListener
Event listeners are created via eventStore.createEventListener(name). They are the simplest way to react to events — ideal for stateless side effects like sending emails, invalidating caches, or logging.
Unlike projections and process managers, event listeners have no checkpoint tracking, no replay support, and no state. They fire for every event as it is emitted.
Creation
Section titled “Creation”const listener = eventStore.createEventListener('notifications')Builder Methods
Section titled “Builder Methods”.withEventHandlers(handlers)
Section titled “.withEventHandlers(handlers)”Registers event handlers named on{EventName}.
listener.withEventHandlers({ async onUserCreated({ payload }) { await sendWelcomeEmail(payload.email) },}).withGlobalEventHandler(handler)
Section titled “.withGlobalEventHandler(handler)”Registers a catch-all handler called for every event.
listener.withGlobalEventHandler(async (event) => { console.log(`Event ${event.type} at position ${event.position}`)}).withAfterEffects(handlers)
Section titled “.withAfterEffects(handlers)”Registers handlers that run after all on{EventName} handlers. Uses after{EventName} naming.
listener.withAfterEffects({ async afterUserDeleted({ streamId }) { await invalidateCache(`user:${streamId}`) },}).withGlobalAfterEffect(handler)
Section titled “.withGlobalAfterEffect(handler)”A catch-all handler that runs after all after-effect handlers.
listener.withGlobalAfterEffect(async (event) => { await auditLog.append(event)})Context Helpers
Section titled “Context Helpers”import { isInsideEventListener, getEventListenerInfo } from '@requence/event-sourcing'| Function | Return Type | Description |
|---|---|---|
isInsideEventListener() | boolean | true if called within an event listener handler. |
getEventListenerInfo() | { name, event } | undefined | Returns the listener name and triggering event. |