Storyboard Listener API
February 17, 2017 ยท View on GitHub
For an example of how to implement a Storyboard listener (plugin), check out the ConsoleListener, arguably the simplest built-in listener. Listeners should follow the API described in this document.
Listener methods
constructor() (optional)
configobject: user configuration options. Should typically be merged with some defaults and stored in the listener instance.contextobject:-
hubobject: the hub instance this listener is attached to. See Hub API for listeners below. -
chalkobject: a pre-configured, always-enabled version of the chalk library. -
mainStoryobject: Storyboard's main story, which can be used to generate logs the same way as a library user:class MyListener { constructor(config, { hub, mainStory }) { this.hub = hub; this.mainStory = mainStory; } init() { this.mainStory.info('myListener', 'Listener is up and running!'); } }
-
getConfig() (mandatory)
- No arguments
- Returns object: current listener configuration.
configure(config) (optional)
- Merges the provided object with the current configuration. You may want to support changes to configuration after initialisation, but this is not mandatory.
configobject: new configuration options.- No return value
init() (optional)
- Executes initialisation operations, e.g. connects to a database, opens a file, etc.
- No arguments
- No return value
tearDown() (optional)
- Executes tear-down operations, e.g. disconnects from a database, closes a file, etc.
- No arguments
- No return value
process() (mandatory)
- Processes a message from the hub, e.g. saves records to a database, writes them to a file, relays them to a browser extension, uploads them to a server, etc.
msgobject: a message from the hub, containing:typestring: the type of message, e.g.RECORDS,LOGIN_REQUEST,LOGIN_RESPONSE,SET_SERVER_FILTER... Typical listeners discard messages withtypeother thanRECORDS.hubIdstring: the ID of the hub to which the originating listener is attached. Typical listeners discard messages from hubs other than the one they are attached to.srcstring: an indication of the message originator, e.g.STORIES(a story),WS_CLIENT(the WebSocket Client listener; used for signalling). When a message is relayed by a listener, it should not change itssrcfield.dataany: the message payload; its format is defined by the messagetype: e.g. an array of log records forRECORDS, a filter string forSET_SERVER_FILTER, clock sync data forCLOCKSY, etc.
- No return value
Listener module
Wrap your listener in a module and export a creator function:
import { addDefaults } from 'timm';
const DEFAULT_CONFIG = { /* ... */ };
const create = (userConfig, context) =>
new MyListener(addDefaults(userConfig, DEFAULT_CONFIG), context);
export default create;
Users can add your listener in the standard way:
import myListener from './myListener';
import { addListener } from 'storyboard';
addListener(myListener, { /* custom config */ });
Hub API for listeners
The listener constructor is passed a reference to some hub methods:
hub.getHubId()
- Provides the ID of the hub our listener is attached to.
- No arguments
- Returns string: hub ID
hub.emitMsg()
- Re-broadcasts a message (that has been previously received via a call to
processMsg()). msgobject: the message to be relayed (contents are described inprocessMsg()above)srcListenerobject: passthisif you don't want to get the message back from the hub when it re-broadcasts it.- No return value
hub.emitMsgWithFields()
- Builds a new message a broadcasts it to all attached listeners.
srcstring: an identifier for the originating listener, e.g.WS_CLIENT.typestring: seemsgcontents inprocessMsg()above.dataany: seemsgcontents inprocessMsg()above.srcListenerobject: passthisif you don't want to get the message back from the hub when it broadcasts it.