0.0.4 • Published 7 years ago
redisemitter v0.0.4
RedisEmitter
Install and test
npm install --save redisemitter
npm test
Setup
const RedisEmitter = require('redisemitter')
const options = {
host: 'localhost',
port: 6379,
index: ['type', 'action', 'name', 'id']
}
const events = new RedisEmitter(options)
options
- host the host redis should connect to.
- default:
'127.0.0.1'
- default:
- port the port redis should connect to.
- default:
6379
- default:
- query the default query all event strings will be generated from.
- default:
['type', 'action', 'name', 'id']
- default:
Emitting events
emit only takes one argument and the event pattern will be built from the message. In this case event:emit:*:*
events.emit({
type: 'event',
action: 'emit'
})
Subscribing to events
Unlike EventEmitter you subscribe to objects instead of strings (a string will be generated from your query). In this case event:*:*:*
events.on({
type: 'event'
})
subscriptions
- on(query, callback) subscribe to all events matching the query
- once(query, callback) subscribe to the first event matching the query
- any(callback) subscribe to any event
Unsubscribing to events
All subscriptions return a disposer that can be called to dispose the event listener.
const dispose = events.on({
type: 'test'
}, (message) => {
console.log(message)
if (message.action === 'quit') {
dispose()
}
})