@lgslabs/bits-message-center v4.0.0
Message Center
The message center utilizeds the event loop and standardizes communication between master and worker resources (e.g. Node Cluster API). Messages are formatted with a type
property for "routing" purposes.
Event Naming Convention
Event names should be unique across the system and namespaced by module name and subsystem. Formatted as follows:
'module-name#SubsystemName eventString'
Example
Module A
has a subsystem server-daemon
that emits a client-added
event. The event string would be:
'module-a#ServerDaemon clientAdded'
Metadata
Some messages contain a metadata object. Metadata typically contains scopes
for permissions and origin
for identifying the origin of the message.
Message Types
All message types are sent through the IPC (message
event) as formatted JSON. Emitter and listener coordination is maintained through expected properties based on message type. For example, MODA can make a request to MODB via the request
message and MODB can respond to MODA via the response
message.
Example
// Message sent from MODA
{
type: 'request',
event: '<event-name>',
requestId: '<unique-id>',
params: [{
scopes: Array or null,
origin: '<message-origin>'
}, ...optional-unique-event-params
]
}
// Message sent from MODB in response
{
type: 'response',
event: '<event-name>',
responseId: '<request-id>',
err: null or Error,
result: Any Type
}
API
addEventListener
Add a listener for a unique event.
Parameter | Type | Description |
---|---|---|
event | String | Unique event name. |
metadata | Object | |
listener | Function | Callback to be invoked upon receiving the event. |
removeEventListener
Remove a listener for a unique event.
Parameter | Type | Description |
---|---|---|
event | String | Unique event name. |
listener | Function | Same function used in addEventListener . |
sendEvent
Notify listeners of a unique event.
Parameter | Type | Description |
---|---|---|
event | String | Unique event name. |
metadata | Object | |
...params | Any Types | Optional, parameters specific to the event data. |
addRequestListener
Add a listener for requests from others.
Parameter | Type | Description |
---|---|---|
event | String | Unique event name. |
metadata | Object | |
listener | Function | Callback to be invoked upon receiving the event. |
removeRequestListener
Remove a listener for requests from others.
Parameter | Type | Description |
---|---|---|
event | String | Unique event name. |
listener | Function | Same function used in addRequestListener . |
sendRequest
Make a request to a request listener.
Parameter | Type | Description |
---|---|---|
event | String | Unique event name. |
metadata | Object | |
...params | Any Types | Optional, parameters specific to the event data. |
addEventSubscriberListener
Add a listener for subscribers to an event. Helpful for filtering messages.
Parameter | Type | Description |
---|---|---|
event | String | Unique event name. |
listener | Function | Callback to be invoked upon receiving the event. |
removeEventSubscriberListener
Remove a listener for subscribers to an event.
Parameter | Type | Description |
---|---|---|
event | String | Unique event name. |
listener | Function | Same function used in addEventSubscriberListener . |
addFirehoseListener
Add a listener to the firehose stream.
Parameter | Type | Description |
---|---|---|
metadata | Object | Metadata object, must contain scopes. |
listener | Function | Callback to be invoked upon receiving the event. |
removeFirehoseListener
Remove a listener from the firehose stream.
Parameter | Type | Description |
---|---|---|
listener | Function | Same function used in addFirehoseListener . |
Testing
Mock
A mock message center is provided, use it in your tests as follows:
const messageCenter = require('@lgslabs/bits-message-center/test/mocks/message-center');