4.0.0 • Published 5 years ago

@lgslabs/bits-message-center v4.0.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 years ago

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.

ParameterTypeDescription
eventStringUnique event name.
metadataObject
listenerFunctionCallback to be invoked upon receiving the event.

removeEventListener

Remove a listener for a unique event.

ParameterTypeDescription
eventStringUnique event name.
listenerFunctionSame function used in addEventListener.

sendEvent

Notify listeners of a unique event.

ParameterTypeDescription
eventStringUnique event name.
metadataObject
...paramsAny TypesOptional, parameters specific to the event data.

addRequestListener

Add a listener for requests from others.

ParameterTypeDescription
eventStringUnique event name.
metadataObject
listenerFunctionCallback to be invoked upon receiving the event.

removeRequestListener

Remove a listener for requests from others.

ParameterTypeDescription
eventStringUnique event name.
listenerFunctionSame function used in addRequestListener.

sendRequest

Make a request to a request listener.

ParameterTypeDescription
eventStringUnique event name.
metadataObject
...paramsAny TypesOptional, parameters specific to the event data.

addEventSubscriberListener

Add a listener for subscribers to an event. Helpful for filtering messages.

ParameterTypeDescription
eventStringUnique event name.
listenerFunctionCallback to be invoked upon receiving the event.

removeEventSubscriberListener

Remove a listener for subscribers to an event.

ParameterTypeDescription
eventStringUnique event name.
listenerFunctionSame function used in addEventSubscriberListener.

addFirehoseListener

Add a listener to the firehose stream.

ParameterTypeDescription
metadataObjectMetadata object, must contain scopes.
listenerFunctionCallback to be invoked upon receiving the event.

removeFirehoseListener

Remove a listener from the firehose stream.

ParameterTypeDescription
listenerFunctionSame 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');
4.0.0

5 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.0.1

5 years ago

2.0.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago