Overview
Service

Services are responsible for handling the background work or handling the communication between client and message broker or any work that you need to run asynchronously with event driven architecture.

Create a Service

A Service is an instance that runs in the background and communicates with other local components by an event bus. Each Service has their own event bus.


The Service using the RxJS (opens in a new tab) Subject as an internal message broker.

@OnStart is required for all service


Interval Service

Interval service is responsible for handling the interval job.

@IntervalService(1000)
export class IntervalService {
    @OnStart()
    start = async () => {
        // logic here
    };
 
    @PreDestroy()
    destroy = async () => {
        // logic here
    };
}

The interval service can stop by calling dispose() method on EventHandler class.


Messaging Service

Messaging service is responsible for handling the communication between Provider and Messaging Broker under internal Event Bus.


@OnStart, @PreDestroy, @Consumer are required. @Publisher is an optional. The purpose of @Publisher is for separate the consuming and publishing behavior.

If the class of MessagingService has only 1 @Consumer and without defining the consumer event name. The default event name is data.

@MessageingService()
export class MessagingService {
    @Publisher()
    start = async () => {
        // logic here
    };
 
    @Consumer('data')
    sendLink = async () => {
        // logic here
    };
 
    @Consumer('refund')
    sendBinary = async () => {
        // logic here
    };
 
    @Consumer('send-noti')
    sendLog = async () => {
        // logic here
    };
 
    @OnStart()
    start = async () => {
        // logic here
    };
 
    @PreDestroy()
    destroy = async () => {
        // logic here
    };
}

Service

It's different with MessagingService, It doesn't have a @Publisher event but they are serve the same purpose.


@OnStart and @PreDestroy are required.

@Service()
export class NormalService {
    @OnStart()
    start = async () => {
        // ...logic here
    };
 
    @Consumer('send-noti')
    start = async () => {
        // ...logic here
    };
 
    @PreDestroy()
    destroy = async () => {
        // destroy logic here
    };
}

Register Services

Add MessagingService,IntervalService,Service classes to @Module({}) services property to enable the services

@Module({
    services: [MessagingService, IntervalService, NormalService],
})
export class AppModule {}

Inject Services

Lookup a service by @EventLookup('name').

@Controller('/eventlookup')
export class HealthCheckControllers {
 
    constructor(@EventLookup('test-messaging') private _event: EventHandler<Any>) {}
}

EventHandler class has 3 methods:

  • emit(name,data) - emit data to an event name data, destroy or a custom event name with data
  • listen(name,handler) - create a new event bus behavior with name and handler
  • dispose() - dispose the event.

dispose() will close all communications of a service so use it wisely.