Real-Time

Event Bus

In-process publish/subscribe with wildcards.

1 min readDocumentationEdit this page

Usage#

typescript
import { EventBus } from '@AxilJS/events'
 
const bus = new EventBus()
 
bus.on('user.created', async (event) => {
  console.log(event.payload)
})
 
await bus.emit('user.created', { id: 1 })

Wildcards#

typescript
bus.on('user.*', handler)  // user.created, user.deleted
bus.on('*', handler)       // everything

One-time and wait#

typescript
bus.once('payment.confirmed', handler)
const event = await bus.waitFor('payment.confirmed', 5000)

Decorators#

typescript
import { OnEvent, registerEventHandlers } from '@AxilJS/events'
 
class Notifications {
  @OnEvent('user.created')
  async sendWelcome(event) { await sendEmail(event.payload.email) }
}
 
registerEventHandlers(new Notifications(), bus)

Help improve the documentation

AxilJS is open source and documentation improvements are welcome.

AxilJS DocumentationMIT License · Built by SyntaxilitY