Skip to main content

EventBus

EventBus

The EventBus is used to globally publish events which can then be subscribed to.

Events are published whenever certain actions take place within the Vendure server, for example:

Using the EventBus it is possible to subscribe to an take action when these events occur. This is done with the .ofType() method, which takes an event type and returns an rxjs observable stream of events:

Example

import { OnApplicationBootstrap } from '@nestjs/common';
import { EventBus, PluginCommonModule, VendurePlugin } from '@vendure/core';
import { filter } from 'rxjs/operators';

@VendurePlugin({
imports: [PluginCommonModule]
})
export class MyPlugin implements OnApplicationBootstrap {

constructor(private eventBus: EventBus) {}

async onApplicationBootstrap() {

this.eventBus
.ofType(OrderStateTransitionEvent)
.pipe(
filter(event => event.toState === 'PaymentSettled'),
)
.subscribe((event) => {
// do some action when this event fires
});
}
}
Signature
class EventBus implements OnModuleDestroy {
constructor(transactionSubscriber: TransactionSubscriber)
publish(event: T) => void;
ofType(type: Type<T>) => Observable<T>;
filter(predicate: (event: VendureEvent) => boolean) => Observable<T>;
}
  • Implements: OnModuleDestroy

constructor

method
(transactionSubscriber: TransactionSubscriber) => EventBus

publish

method
(event: T) => void

Publish an event which any subscribers can react to.

ofType

method
(type: Type<T>) => Observable<T>

Returns an RxJS Observable stream of events of the given type. If the event contains a RequestContext object, the subscriber will only get called after any active database transactions are complete.

This means that the subscriber function can safely access all updated data related to the event.

filter

method
(predicate: (event: VendureEvent) => boolean) => Observable<T>

Returns an RxJS Observable stream of events filtered by a custom predicate. If the event contains a RequestContext object, the subscriber will only get called after any active database transactions are complete.

This means that the subscriber function can safely access all updated data related to the event.