Cross-server eventing
npm install server-events
js
const ServerEvents = require('server-events');
const randomEvents = new ServerEvents({
host: 'localhost',
port: 6379,
db: 0
});
// connect to redis server
await randomEvents.init()
randomEvents.onerror(function(store, error) {
// store can be either 'outputStore' or 'inputStore'
console.log(store, error);
});
`
$3
Events are referenced by name and id. The name specifies context while id can be used to reference a certain object (i.e. database table index).
`js
randomEvents.emit('not so random', 12);
`
Additional arguments passed to the emitter will be passed on to the listener.
`js
await randomEvents.emit('pi event', 314, 'arg1', 1, {type: 'object'});
`
$3
Events returned as promises. Provide a thenable if you wish to get the results as an array.
`js
randomEvents.on('pi event', 314).then(function(result) {
// result ~ ['arg1', 1, {type: 'object'}]
});
`
Or spreadable to receive them naturally.
`js
randomEvents.on('pi event', 314).then(function([a, b, c]) {
// a ~ 'arg1', b ~ 1, c ~ {type: 'object'}
});
`
Event timeout will result in an error.
`js
randomEvents.on('wont', 'happen').catch(function(error) {
// event has timed out
});
`
Custom timeout can be passed.
`js
randomEvents.on('ev', 101, 5000).catch(function(a) {
// event will timeout after 5000 ms unless result is returned
});
`
$3
The default timeout for all ServerEvents instances is 10000 ms. This can be overriden for each instance:
`js
randomEvents.timeout = 1000; // 1 second
`
Test
`js
npm test
``