1.1.0 • Published 4 years ago

@nestwealth/service-bus v1.1.0

Weekly downloads
-
License
-
Repository
-
Last release
4 years ago

@nestwealth/service-bus

Nest service bus is the service bus architecture for the event driven (SOA 2.0) for nestwealth platform. Service Bus implements a communication system between different services in a service-oriented architecture (SOA 2.0). It is a software architecture for distributed computing, promotes agility and flexibility with regard to high-level protocol communication between applications.

How to use NestServiceBus in Nest SOA 2.0 Architecture

Importing NestServiceBus into your service

import { ServiceBus } from '@nestwealth/service-bus';

Subscribing to NestServiceBus

Subscribing to NestServiceBus means that when the service gets up and running, it gets attached to the service bus. By attaching to service bus, the service creates a new queue, and is capable to receiving all the events. In the context of the above diagram, the Q is the one with the <SERVICE_NAME> and the service gets bind to this queue, and works as a consumer.

await ServiceBus.subscribe('<SERVICE_NAME>');

Emit Events to Service Bus

Emitting events to service bus means that the service wants to send out an event on to the service bus which can be consumed by the services required to be listened to.

await ServiceBus.emit({
  event: 'DEMO_SERVICE_2_EVENT_EMIT',
  metadata: {
  orgSlug: 'nestwealth'
  },
  content: {
    a: 'b'
  }
});

Please note: Currently the above Event architecture is still WIP. This is just an illustration and not the final event structure

Listen to Events on Service Bus

In the example below, the service is listening to all the Events being passed on to the queue it is subscribed to. The Rules engine will decide if this service is required to process the event, otherwise it can ignore it.

return ServiceBus.listen((event: any) => {
  // RULES ENGINE HERE
  
  // LISTENING TO THE EVENT MESSAGE
  console.log(`message received on service ${service.name}`);
  console.log(JSON.parse(event.content.toString()));
  // Acknowledge the event
  ServiceBus.acknowledgeMessage(event);
});

Please note: In the RabbitMQ implementation of the service bus, currently there is no rules engine assigned on the service bus level but at the service level itself. This brings the responsibility down to the service to implement rules and listen or ignore the messages, till the time we don’t introduce the events at the service level.

Unsubscribe to NestServiceBus

Unsubscribing to NestServiceBus is required when you shut down your service, so that we can stop the flow the events when the service is down. This should not be confused with service crashing, but when essentially the service is shut down thereby closing the connection to the queue. Closing the connection will not remove the queue and will still store the events to the Queue, and when the service is restarted again or is back online, it will start processing the events again.

This will also help in scaling the system, as in, if required we can setup multiple instances (using ServiceBus.subscribe() method) of these services which can process events in parallel.

ServiceBus.unsubscribe();