NodeJS Event Manager
A NodeJS Event Manager using builtin NodeJS Events.
The main purpose of this package, is to facilitate the use of events, in a node JS application.
It implements the same "api" ("interface") as rabbitmq-event-manager so you can easily switch from NodeJS to RabbitMQ of vice-versa.
It can be usefull for testing purpose, but also in order to start quickly with nodeJS and if your application is getting bigger and you want to externalize some parts, you can deploy a RabittMQ and make your applications communicate together
Install
npm install nodejs-event-manager
Or with Yarn
yarn add nodejs-event-manager
Basic Example
- Initialize
;const myEventManager = ;myEventManager ;
- Consumer
;const myEventManager = appName:'CONSUMER');myEventManager;
- Producer
;const myEventManager = appName: "PRODUCER_1" ; myEventManager;
NOTE: ⚠️ A very good convention may be to prefix the name of the event with the emitter application name, for example :
PRODUCER_1.MY_EVENT_NAME
but it's not mandatory.
Then we can create new Consumer and listen the same event :
;const myEventManager = appName:'OTHER_CONSUMER');myEventManager;
Options
Name | Type | Default | Description |
---|---|---|---|
appName | String | - | The name of the application (used for naming exchanges and queues). |
metas | boolean or (function) | true | Weither or not to add _metas infirmations in the event, If a function this returned value, will become the _metas object (see ) |
logPrefix | string | [RABBITMQ] | The text that will be printed before the error log |
logLevel | string | error | The log Level (see winston logLevels) |
logTransportMode | string | console | Mute (no log), or output to console. Possible values are ("console" or "mute") |
defaultResponseSuffix | string | '.RESPONSE' | The suffix to add to the response event name. |
emitAndWaitTimeout | number | 30000 | the timeout when emit and wait |
Metas Informations
By defaut, some metas data are added to the payload :
- guid : A unique id generated, to be able to debug for example, or for following the event.
- timestamp : A number of milliseconds elapsed since January 1, 1970 00:00:00 UTC. (
Date.now()
) - name : A string which is the name of the emitted event.
- applicationName: The value of the application which emits the Event.
So if your payload is :
userId: 42;
With Metas data it will be :
_metas: guid: '465e008c-d37f-4e31-b494-023e6d187946' name: 'MY_EVENT_NAME' timestamp: 1519211809934 applicationName: 'PRODUCER_1' userId:42
You can remove metas informations by settings the option value "metas" to false.
You can also override the metas generation by giving a function as metas options value (on the emitter side only, as the event is generated there).
With no metas
;const myEventManagerWithNoMetas = url: "amqp://localhost" appName: "PRODUCER_1" metas: false;const payload = userId: 42 ;myEventManagerWithNoMetas;// Payload will be// {// userId:42// }
Override Metas
;const myEventManagerOverrideMetas = url: "amqp://localhost" appName: "PRODUCER_1" { // sourceMetas contains the default metaa return ...sourceMetas otherProperty: "MyValue" ; };const payload = userId: 42 ;myEventManagerOverrideMetas;// Payload will be// {// _metas: {// guid : '465e008c-d37f-4e31-b494-023e6d187947'// name: 'MY_EVENT_NAME',// timestamp: 1519211809934,// otherProperty:'MyValue'// }// userId:42// }
Emit and wait
- since version 1.1.0 An example with a test :
;