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.
config
object: user configuration options. Should typically be merged with some defaults and stored in the listener instance.context
object:-
hub
object: the hub instance this listener is attached to. See Hub API for listeners below. -
chalk
object: a pre-configured, always-enabled version of the chalk library. -
mainStory
object: 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!'); } }
-
- No arguments
- Returns object: current listener configuration.
- Merges the provided object with the current configuration. You may want to support changes to configuration after initialisation, but this is not mandatory.
config
object: new configuration options.- No return value
- Executes initialisation operations, e.g. connects to a database, opens a file, etc.
- No arguments
- No return value
- Executes tear-down operations, e.g. disconnects from a database, closes a file, etc.
- No arguments
- No return value
- 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.
msg
object: a message from the hub, containing:type
string: the type of message, e.g.RECORDS
,LOGIN_REQUEST
,LOGIN_RESPONSE
,SET_SERVER_FILTER
... Typical listeners discard messages withtype
other thanRECORDS
.hubId
string: 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.src
string: 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 itssrc
field.data
any: 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
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 */ });
The listener constructor is passed a reference to some hub methods:
- Provides the ID of the hub our listener is attached to.
- No arguments
- Returns string: hub ID
- Re-broadcasts a message (that has been previously received via a call to
processMsg()
). msg
object: the message to be relayed (contents are described inprocessMsg()
above)srcListener
object: passthis
if you don't want to get the message back from the hub when it re-broadcasts it.- No return value
- Builds a new message a broadcasts it to all attached listeners.
src
string: an identifier for the originating listener, e.g.WS_CLIENT
.type
string: seemsg
contents inprocessMsg()
above.data
any: seemsg
contents inprocessMsg()
above.srcListener
object: passthis
if you don't want to get the message back from the hub when it broadcasts it.