@jcoreio/redis-subscriber
v1.0.6
Published
subscribe specific callbacks to specific channels on the same redis client
Downloads
9
Readme
redis-subscriber
Subscribe specific callbacks to specific channels on the same redis client
Usage
import redis from 'redis'
import RedisSubscriber from '@jcoreio/redis-subscriber'
const client = redis.createClient()
const subscriber = new RedisSubscriber(client)
Subscribing
subscriber.subscribe('foo', (channel, message) => ...)
subscriber.psubscribe('foo/*', (pattern, channel, message) => ...)
Unsubscribing
There are two ways to unsubscribe:
- call
unsubscribe
orpunsubscribe
const onMessage = (channel, message) => ...
const onPMessage = (pattern, channel, message) => ...
subscriber.subscribe('foo', onMessage)
subscriber.psubscribe('foo/*', onPMessage)
...
subscriber.unsubscribe('foo', onMessage)
subscriber.unsubscribe('foo/*', onPMessage)
- call the function returned by
subscribe
orpsubscribe
const unsubscribe = subscriber.subscribe('foo', onMessage)
unsubscribe()
const unsubscribe = subscriber.psubscribe('foo/*', onPMessage)
unsubscribe()
parseMessage
option
Parse all messages as JSON before sending them to callbacks:
const subscriber = new RedisSubscriber(client, {parseMessage: JSON.parse})