Skip to content

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.

const listener = eventStore.createEventListener('notifications')

Registers event handlers named on{EventName}.

listener.withEventHandlers({
async onUserCreated({ payload }) {
await sendWelcomeEmail(payload.email)
},
})

Registers a catch-all handler called for every event.

listener.withGlobalEventHandler(async (event) => {
console.log(`Event ${event.type} at position ${event.position}`)
})

Registers handlers that run after all on{EventName} handlers. Uses after{EventName} naming.

listener.withAfterEffects({
async afterUserDeleted({ streamId }) {
await invalidateCache(`user:${streamId}`)
},
})

A catch-all handler that runs after all after-effect handlers.

listener.withGlobalAfterEffect(async (event) => {
await auditLog.append(event)
})
import { isInsideEventListener, getEventListenerInfo } from '@requence/event-sourcing'
FunctionReturn TypeDescription
isInsideEventListener()booleantrue if called within an event listener handler.
getEventListenerInfo(){ name, event } | undefinedReturns the listener name and triggering event.