@mavonic/ms-event-manager
v1.0.1
Published
Metaservice event manager
Downloads
844
Keywords
Readme
Meta Service Event Manager
MS Event Manager is a Fastify plugin that provides an easy way to use RabbitMQ for event bus applications.
Installation
To install the plugin, you need to link it to your project:
- Navigate to the
ms-event-manager
directory and run:
npm link
- Navigate to your project directory and run:
npm link ms-event-manager
Usage
First, import the plugin in your project:
import msEventManager from 'ms-event-manager';
Then, register the plugin with Fastify and provide the necessary options:
fastify.register(msEventManager, {
connectionStrings: 'your-connection-string',
exchange: 'your-exchange',
queueName: 'your-queue-name',
durable: true,
prefetchCount: 1
});
Replace 'your-connection-string', 'your-exchange', and 'your-queue-name' with your actual RabbitMQ connection string, exchange, and queue name.
Options
The plugin accepts the following options:
- connectionStrings: The connection string to your RabbitMQ server.
- exchange: The name of the exchange.
- queueName: The name of the queue.
- durable: Whether the queue is durable (i.e., it survives a broker restart).
- prefetchCount: The maximum number of messages sent over the channel that can be awaiting acknowledgement.
Event Creation Process
Creating a new event involves several steps:
Add the routing key: Add a new routing key for your event in the
routingKeys.ts
file. The routing key should be a string that uniquely identifies your event.Create an interface: Create a new interface for your event in the
interfaces
directory. The interface should define the shape of the data that your event will carry. For example, if your event carries a user ID and a username, your interface might look like this:
export interface MyEvent {
routingKey: RoutingKeys.USER_LOGIN;
message: {
id: number;
name: string;
};
}
Export the interface: Add an export for your new interface in the index.ts file in the interfaces directory. This allows the interface to be imported from other parts of your project.
Create a file for the service: Create a new file in the services directory for your service. This file should export a function that handles your event. The function should take an instance of your event interface as a parameter.
Remember to name your files according to the service they correspond to. This helps to keep your code organized and makes it easier to find the code related to a specific service.
After you've completed these steps, your new event will be ready to use!